PHP中怎么操作文件與目錄-創(chuàng)新互聯(lián)

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

目前成都創(chuàng)新互聯(lián)公司已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機、網(wǎng)站托管運營、企業(yè)網(wǎng)站設(shè)計、六安網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

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


<?php
// 輸出目錄中的文件
function outputcurfiles ($allowedtypes, $thedir){
//首先,我們確保目錄存在。
if (is_dir ($thedir)){
 //現(xiàn)在,我們使用scandir掃描目錄中的文件。
 $scanarray = scandir ($thedir);
 //接著我們開始解析數(shù)組。
 //scandir()用“.”和“..”統(tǒng)計文件導(dǎo)航列表
 //因此作為文件,我們不應(yīng)該列出他們。
 for ($i = 0; $i < count ($scanarray); $i++){
  if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
   //現(xiàn)在,進行檢查,以確保這是一個文件,而不是一個目錄。
   if (is_file ($thedir . "/" . $scanarray[$i])){
    //現(xiàn)在,因為我們將允許客戶端編輯這個文件,
    //我們必須檢查它是否是可讀和可寫。
    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 "對不起,這個目錄不存在.";
}
}
$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)計目錄中包含“.”或“..”的情況
        if ($anitem != "." && $anitem != ".."){
          //此時如果是一個目錄,則縮進一點
          //再去遞歸
          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)){
            //此時輸出文件.
            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)計文件導(dǎo)航列表
    //因此作為文件,我們不應(yīng)該列出他們.
    for ($i = 0; $i < count ($scanarray); $i++){
      if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
        //現(xiàn)在,我們檢查,以確保這是一個文件,而不是一個目錄.
        if (is_file ($thedir . "/" . $scanarray[$i])){
          //現(xiàn)在需要做的是循環(huán)數(shù)據(jù)到一個關(guān)聯(lián)數(shù)組.
          $finalarray[$thedir . "/" . $scanarray[$i]] = filemtime ($thedir . "/" . $scanarray[$i]);
        }
      }
    }
    //至此,我們已經(jīng)遍歷了整個數(shù)組,現(xiàn)在需要做的只是asort()它。
    asort ($finalarray);
    return ($finalarray);
  } else {
    echo "對不起,這個目錄不存在.";
  }
}
//然后,我們將函數(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)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

分享題目:PHP中怎么操作文件與目錄-創(chuàng)新互聯(lián)
文章出自:http://muchs.cn/article14/ddsege.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、建站公司、域名注冊云服務(wù)器、動態(tài)網(wǎng)站、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)