《PHP實例:PHP文件與目錄操作示例》要點:
本文介紹了PHP實例: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
?><div style="margin-left: 10px;" mce_style="margin-left:10px;"><?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();
//然后開始解析數組
//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相關內容感興趣的讀者可查看本站專題:《PHP目錄操作技巧匯總》、《php文件操作總結》、《PHP數組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php面向對象程序設計入門教程》、《PHP網絡編程技巧總結》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
PHP編程希望本文所述對大家PHP程序設計有所幫助.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/2244.html