《PHP實(shí)例:PHP文件與目錄操作示例》要點(diǎn):
本文介紹了PHP實(shí)例:PHP文件與目錄操作示例,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP編程本文實(shí)例講述了PHP文件與目錄操作.分享給大家供大家參考,具體如下:
PHP編程文件目錄相關(guān)函數(shù)
PHP編程
<?php
// 輸出目錄中的文件
function outputcurfiles ($allowedtypes, $thedir){
//首先,我們確保目錄存在.
if (is_dir ($thedir)){
//現(xiàn)在,我們使用scandir掃描目錄中的文件.
$scanarray = scandir ($thedir);
//接著我們開始解析數(shù)組.
//scandir()用“.”和“..”統(tǒng)計(jì)文件導(dǎo)航列表
//因此作為文件,我們不應(yīng)該列出他們.
for ($i = 0; $i < count ($scanarray); $i++){
if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
//現(xiàn)在,進(jìn)行檢查,以確保這是一個(gè)文件,而不是一個(gè)目錄.
if (is_file ($thedir . "/" . $scanarray[$i])){
//現(xiàn)在,因?yàn)槲覀儗⒃试S客戶端編輯這個(gè)文件,
//我們必須檢查它是否是可讀和可寫.
if (is_writable ($thedir. "/" . $scanarray[$i]) && is_readable($thedir . "/" . $scanarray[$i])){
//現(xiàn)在,我們檢查文件類型是否存在于允許的類型數(shù)組中.
$thepath = pathinfo ($thedir . "/" . $scanarray[$i]);
if (in_array ($thepath['extension'], $allowedtypes)){
//如果文件符合規(guī)定,我們可以繼續(xù)輸出.
echo $scanarray[$i] . "<br />";
}
}
}
}
}
} else {
echo "對(duì)不起,這個(gè)目錄不存在.";
}
}
$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))){
//不統(tǒng)計(jì)目錄中包含“.”或“..”的情況
if ($anitem != "." && $anitem != ".."){
//此時(shí)如果是一個(gè)目錄,則縮進(jìn)一點(diǎn)
//再去遞歸
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)){
//此時(shí)輸出文件.
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();
//然后開始解析數(shù)組
//scandir()用“.”和“..”統(tǒng)計(jì)文件導(dǎo)航列表
//因此作為文件,我們不應(yīng)該列出他們.
for ($i = 0; $i < count ($scanarray); $i++){
if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
//現(xiàn)在,我們檢查,以確保這是一個(gè)文件,而不是一個(gè)目錄.
if (is_file ($thedir . "/" . $scanarray[$i])){
//現(xiàn)在需要做的是循環(huán)數(shù)據(jù)到一個(gè)關(guān)聯(lián)數(shù)組.
$finalarray[$thedir . "/" . $scanarray[$i]] = filemtime ($thedir . "/" . $scanarray[$i]);
}
}
}
//至此,我們已經(jīng)遍歷了整個(gè)數(shù)組,現(xiàn)在需要做的只是asort()它.
asort ($finalarray);
return ($finalarray);
} else {
echo "對(duì)不起,這個(gè)目錄不存在.";
}
}
//然后,我們將函數(shù)指向我們需要查看的目錄.
$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編程更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP目錄操作技巧匯總》、《php文件操作總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
PHP編程希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助.
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/2244.html