<address id="ousso"></address>
<form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
  1. php語言

    PHP文件與目錄操作的方法

    時間:2025-05-01 09:30:09 php語言 我要投稿
    • 相關推薦

    PHP文件與目錄操作的方法

      主要介紹了PHP文件與目錄操作,涉及php針對文件與目錄的遍歷、判斷與排序相關操作技巧,注釋中備有較為詳細的說明,需要的朋友可以參考下。

      本文實例講述了PHP文件與目錄操作。分享給大家供大家參考,具體如下:

      文件目錄相關函數

      <?php

      // 輸出目錄中的文件

      function outputcurfiles ($allowedtypes, $thedir){

      //首先,我們確保目錄存在。

      if (is_dir ($thedir)){

      //現在,我們使用scandir掃描目錄中的文件。

      $scanarray = scandir ($thedir);

      //接著我們開始解析數組。

      //scandir()用“.”和“..”統計文件導航列表

      //因此作為文件,我們不應該列出他們。

      for ($i = 0; $i < count ($scanarray); $i++){

      if ($scanarray[$i] != "." && $scanarray[$i] != ".."){

      //現在,進行檢查,以確保這是一個文件,而不是一個目錄。

      if (is_file ($thedir . "/" . $scanarray[$i])){

      //現在,因為我們將允許客戶端編輯這個文件,

      //我們必須檢查它是否是可讀和可寫。

      if (is_writable ($thedir. "/" . $scanarray[$i]) &&  is_readable($thedir . "/" . $scanarray[$i])){

      //現在,我們檢查文件類型是否存在于允許的類型數組中.

      $thepath = pathinfo ($thedir . "/" . $scanarray[$i]);

      if (in_array ($thepath['extension'], $allowedtypes)){

      //如果文件符合規定,我們可以繼續輸出.

      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))){

      //不統計目錄中包含“.”或“..”的情況

      if ($anitem != "." && $anitem != ".."){

      //此時如果是一個目錄,則縮進一點

      //再去遞歸

      if (is_dir ($thedir . "/" . $anitem)){

      ?><span style="font-weight: bold;" mce_style="font-weight: bold;"><?php echo $anitem; ?></span><?php

      ?><P style="margin-left: 10px;" mce_style="margin-left:10px;"><?php

      recurdir ($thedir . "/" . $anitem );

      ?></P><?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();

      //然后開始解析數組

      //scandir()用“.”和“..”統計文件導航列表

      //因此作為文件,我們不應該列出他們.

      for ($i = 0; $i < count ($scanarray); $i++){

      if ($scanarray[$i] != "." && $scanarray[$i] != ".."){

      //現在,我們檢查,以確保這是一個文件,而不是一個目錄.

      if (is_file ($thedir . "/" . $scanarray[$i])){

      //現在需要做的是循環數據到一個關聯數組.

      $finalarray[$thedir . "/" . $scanarray[$i]] = filemtime ($thedir . "/" . $scanarray[$i]);

      }

      }

      }

      //至此,我們已經遍歷了整個數組,現在需要做的只是asort()它。

      asort ($finalarray);

      return ($finalarray);

      } else {

      echo "對不起,這個目錄不存在.";

      }

      }

      //然后,我們將函數指向我們需要查看的目錄.

      $sortedarray = sortfilesbydate ("testfolder");

      //至此,就可以按照如下形式輸出:

      while ($element = each ($sortedarray)){

      echo "File: " . $element['key'] . " was last modified: " . date ("F j, Y h:i:s", $element['value']) . "<br />";

      }

      ?>

    【PHP文件與目錄操作的方法】相關文章:

    php中目錄文件操作詳談09-20

    PHP遍歷目錄文件常用方法09-23

    php操作excel文件的方法小結11-05

    PHP開發:linux 父目錄權限影響子目錄文件操作08-12

    PHP文件怎么操作09-03

    php查找指定目錄下指定大小文件的方法08-03

    php文件操作函數解釋11-07

    PHP常用的文件操作函數10-17

    用PHP遍歷目錄下的全部文件06-13

    <address id="ousso"></address>
    <form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
    1. 日日做夜狠狠爱欧美黑人