糯米文學吧

位置:首頁 > 計算機 > php語言

PHP文件與目錄操作的方法

php語言4.16K

主要介紹了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 目錄 文件