溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP中怎么操作文件與目錄

發(fā)布時(shí)間:2021-06-23 16:55:26 來源:億速云 閱讀:113 作者:Leah 欄目:開發(fā)技術(shù)

PHP中怎么操作文件與目錄,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

文件目錄相關(guān)函數(shù)

<?php
// 輸出目錄中的文件
function outputcurfiles ($allowedtypes, $thedir){
//首先,我們確保目錄存在。
if (is_dir ($thedir)){
 //現(xiàn)在,我們使用scandir掃描目錄中的文件。
 $scanarray = scandir ($thedir);
 //接著我們開始解析數(shù)組。
 //scandir()用“.”和“..”統(tǒng)計(jì)文件導(dǎo)航列表
 //因此作為文件,我們不應(yīng)該列出他們。
 for ($i = 0; $i < count ($scanarray); $i++){
  if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
   //現(xiàn)在,進(jìn)行檢查,以確保這是一個(gè)文件,而不是一個(gè)目錄。
   if (is_file ($thedir . "/" . $scanarray[$i])){
    //現(xiàn)在,因?yàn)槲覀儗⒃试S客戶端編輯這個(gè)文件,
    //我們必須檢查它是否是可讀和可寫。
    if (is_writable ($thedir. "/" . $scanarray[$i]) &&  is_readable($thedir . "/" . $scanarray[$i])){
     //現(xiàn)在,我們檢查文件類型是否存在于允許的類型數(shù)組中.
     $thepath = pathinfo ($thedir . "/" . $scanarray[$i]);
     if (in_array ($thepath['extension'], $allowedtypes)){
      //如果文件符合規(guī)定,我們可以繼續(xù)輸出.
      echo $scanarray[$i] . "<br />";
     }
    }
   }
  }
 }
} else {
 echo "對(duì)不起,這個(gè)目錄不存在.";
}
}
$allowedtypes = array ("txt","html");
outputcurfiles ($allowedtypes, "testfolder");
///////////////////////////////////////////////////
function recurdir ($thedir) {
  //First attempt to open the directory.
  try {
    if ($adir = opendir ($thedir)){
      //掃描目錄。
      while (false !== ($anitem = readdir ($adir))){
        //不統(tǒng)計(jì)目錄中包含“.”或“..”的情況
        if ($anitem != "." && $anitem != ".."){
          //此時(shí)如果是一個(gè)目錄,則縮進(jìn)一點(diǎn)
          //再去遞歸
          if (is_dir ($thedir . "/" . $anitem)){
            ?><span  mce_><?php echo $anitem; ?></span><?php
            ?><div  mce_><?php
            recurdir ($thedir . "/" . $anitem );
            ?></div><?php
          } elseif (is_file ($thedir . "/" . $anitem)){
            //此時(shí)輸出文件.
            echo $anitem . "<br />";
          }
        }
      }
    } else {
      throw new exception ("Sorry, directory could not be openend.");
    }
  } catch (exception $e) {
    echo $e->getmessage();
  }
}
echo "<br />/////////////////////////////////////<br /><br />";
recurdir("testfolder");
//////////////////////////////////////////////////////////////////
echo "<br />/////////////////////////////////////<br /><br />";
function sortfilesbydate ($thedir){
  //首先,需要確保目錄存在。
  if (is_dir ($thedir)){
    //接著,我們使用scandir掃描此目錄中的文件.
    $scanarray = scandir ($thedir);
    $finalarray = array();
    //然后開始解析數(shù)組
    //scandir()用“.”和“..”統(tǒng)計(jì)文件導(dǎo)航列表
    //因此作為文件,我們不應(yīng)該列出他們.
    for ($i = 0; $i < count ($scanarray); $i++){
      if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
        //現(xiàn)在,我們檢查,以確保這是一個(gè)文件,而不是一個(gè)目錄.
        if (is_file ($thedir . "/" . $scanarray[$i])){
          //現(xiàn)在需要做的是循環(huán)數(shù)據(jù)到一個(gè)關(guān)聯(lián)數(shù)組.
          $finalarray[$thedir . "/" . $scanarray[$i]] = filemtime ($thedir . "/" . $scanarray[$i]);
        }
      }
    }
    //至此,我們已經(jīng)遍歷了整個(gè)數(shù)組,現(xiàn)在需要做的只是asort()它。
    asort ($finalarray);
    return ($finalarray);
  } else {
    echo "對(duì)不起,這個(gè)目錄不存在.";
  }
}
//然后,我們將函數(shù)指向我們需要查看的目錄.
$sortedarray = sortfilesbydate ("testfolder");
//至此,就可以按照如下形式輸出:
while ($element = each ($sortedarray)){
  echo "File: " . $element['key'] . " was last modified: " . date ("F j, Y h:i:s", $element['value']) . "<br />";
}
?>

關(guān)于PHP中怎么操作文件與目錄問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI