PHP檔案與目錄操作

來源:互聯網
上載者:User
這篇文章主要介紹了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 />";}?>

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.