PHP文件與目錄操作的方法
主要介紹了PHP文件與目錄操作,涉及php針對文件與目錄的遍歷、判斷與排序相關(guān)操作技巧,注釋中備有較為詳細的說明,需要的朋友可以參考下。
本文實例講述了PHP文件與目錄操作。分享給大家供大家參考,具體如下:
文件目錄相關(guān)函數(shù)
<?php
/pic/p>
function outputcurfiles ($allowedtypes, $thedir){
/pic/p>
if (is_dir ($thedir)){
/pic/p>
$scanarray = scandir ($thedir);
/pic/p>
/pic/p>
/pic/p>
for ($i = 0; $i < count ($scanarray); $i++){
if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
/pic/p>
if (is_file ($thedir . "/" . $scanarray[$i])){
/pic/p>
/pic/p>
if (is_writable ($thedir. "/" . $scanarray[$i]) && is_readable($thedir . "/" . $scanarray[$i])){
/pic/p>
$thepath = pathinfo ($thedir . "/" . $scanarray[$i]);
if (in_array ($thepath['extension'], $allowedtypes)){
/pic/p>
echo $scanarray[$i] . "<br />";
}
}
}
}
}
} else {
echo "對不起,這個目錄不存在.";
}
}
$allowedtypes = array ("txt","html");
outputcurfiles ($allowedtypes, "testfolder");
/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/
function recurdir ($thedir) {
/pic/p>
try {
if ($adir = opendir ($thedir)){
/pic/p>
while (false !== ($anitem = readdir ($adir))){
/pic/p>
if ($anitem != "." && $anitem != ".."){
/pic/p>
/pic/p>
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)){
/pic/p>
echo $anitem . "<br />";
}
}
}
} else {
throw new exception ("Sorry, directory could not be openend.");
}
} catch (exception $e) {
echo $e->getmessage();
}
}
echo "<br />/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/<br /><br />";
recurdir("testfolder");
/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/p>
echo "<br />/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/pic/<br /><br />";
function sortfilesbydate ($thedir){
/pic/p>
if (is_dir ($thedir)){
/pic/p>
$scanarray = scandir ($thedir);
$finalarray = array();
/pic/p>
/pic/p>
/pic/p>
for ($i = 0; $i < count ($scanarray); $i++){
if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
/pic/p>
if (is_file ($thedir . "/" . $scanarray[$i])){
/pic/p>
$finalarray[$thedir . "/" . $scanarray[$i]] = filemtime ($thedir . "/" . $scanarray[$i]);
}
}
}
/pic/p>
asort ($finalarray);
return ($finalarray);
} else {
echo "對不起,這個目錄不存在.";
}
}
/pic/p>
$sortedarray = sortfilesbydate ("testfolder");
/pic/p>
while ($element = each ($sortedarray)){
echo "File: " . $element['key'] . " was last modified: " . date ("F j, Y h:i:s", $element['value']) . "<br />";
}
?>
【PHP文件與目錄操作的方法】相關(guān)文章:
php中目錄文件操作詳談02-21
PHP遍歷目錄文件常用方法03-06
PHP開發(fā):linux 父目錄權(quán)限影響子目錄文件操作02-13
PHP文件怎么操作01-29
php查找指定目錄下指定大小文件的方法09-11
PHP常用的文件操作函數(shù)10-24
php文件操作函數(shù)解釋02-09
用PHP遍歷目錄下的全部文件03-17