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開發的安全問題
p給了開發者極大的靈活性,但是這也為安全問題帶來了潛在的隱患,下面是小編分享的PHP開發的安全問題,一起來看一下吧。安全保護一般性要點不相信表單對於一般的Javascript前台驗證,由於無法得知用户的行為,例如關閉了瀏覽器的javascript引擎,這樣通過POST惡意數據到服...
-
php二維數組相同鍵名相加實例
導語:在php二維數組雷同鍵名相加,你寫嗎?下面的是本站小編為大家蒐集的php二維數組相同鍵名相加實例,希望對你會有所幫助。Array([uid]=>19[pid]=>Array([0]=>91[1]=>81)[price]=>Array([0]=>6[1]=>14)[pnum]=>Array([0]=>1[1]=>1))求相同鍵名的值相加如(price的相...
-
php怎麼生成隨機密碼
使用PHP開發應用程序,尤其是網站程序,常常需要生成隨機密碼,如用户註冊生成隨機密碼,用户重置密碼也需要生成一個隨機的密碼。隨機密碼也就是一串固定長度的字符串,下面小編收集整理了幾種生成隨機字符串的'方法,以供大家參考。方法一:1、在33–126中生成一個隨機整...
-
最實用的PHP函數10個
以下是本站小編搜索整理的關於最實用的PHP函數10個,供參考借鑑,希望對大家有所幫助!想了解更多相關信息請持續關注我們應屆畢業生考試網!1.sys_getloadavg()sys_getloadavt()可以獲得系統負載情況。該函數返回一個包含三個元素的數組,每個元素分別代表系統再過去...