PHP文件與目錄操作示例
本文實(shí)例講述了PHP文件與目錄操作。分享給大家供大家參考,具體如下:
文件目錄相關(guān)函數(shù)
<?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)行檢查,以確保這是一個文件,而不是一個目錄。 if (is_file ($thedir . "/" . $scanarray[$i])){ //現(xiàn)在,因?yàn)槲覀儗⒃试S客戶端編輯這個文件, //我們必須檢查它是否是可讀和可寫。 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 "對不起,這個目錄不存在."; } } $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í)如果是一個目錄,則縮進(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)在,我們檢查,以確保這是一個文件,而不是一個目錄. if (is_file ($thedir . "/" . $scanarray[$i])){ //現(xiàn)在需要做的是循環(huán)數(shù)據(jù)到一個關(guān)聯(lián)數(shù)組. $finalarray[$thedir . "/" . $scanarray[$i]] = filemtime ($thedir . "/" . $scanarray[$i]); } } } //至此,我們已經(jīng)遍歷了整個數(shù)組,現(xiàn)在需要做的只是asort()它。 asort ($finalarray); return ($finalarray); } else { echo "對不起,這個目錄不存在."; } } //然后,我們將函數(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 />"; } ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP目錄操作技巧匯總》、《php文件操作總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
- PHP中addslashes()和stripslashes()實(shí)現(xiàn)字符串轉(zhuǎn)義和還原用法實(shí)例
- PHP實(shí)現(xiàn)的文件操作類及文件下載功能示例
- PHP數(shù)組操作實(shí)例分析【添加,刪除,計(jì)算,反轉(zhuǎn),排序,查找等】
- PHP常見字符串處理函數(shù)用法示例【轉(zhuǎn)換,轉(zhuǎn)義,截取,比較,查找,反轉(zhuǎn),切割】
- PHP會話控制實(shí)例分析
- PHP面向?qū)ο蟪绦蛟O(shè)計(jì)方法實(shí)例詳解
- PHP數(shù)據(jù)庫處理封裝類實(shí)例
- 如何判斷php mysqli擴(kuò)展類是否開啟
- Bootstrap php制作動態(tài)分頁標(biāo)簽
- Thinkphp框架中D方法與M方法的區(qū)別
- php的4種常用運(yùn)行方式詳解
- php 反斜杠處理函數(shù)addslashes()和stripslashes()實(shí)例詳解
相關(guān)文章
php實(shí)現(xiàn)隨機(jī)生成易于記憶的密碼
這篇文章主要介紹了php實(shí)現(xiàn)隨機(jī)生成易于記憶的密碼,實(shí)例分析了php生成隨機(jī)密碼的相關(guān)技巧,需要的朋友可以參考下2015-06-06PHP操作文件類的函數(shù)代碼(文件和文件夾創(chuàng)建,復(fù)制,移動和刪除)
PHP操作文件類(文件和文件夾創(chuàng)建,復(fù)制,移動和刪除) ,使用也比較方便,需要的朋友可以參考下。2011-11-11PHP 實(shí)現(xiàn)explort() 功能的詳解
本篇文章是對PHP 實(shí)現(xiàn)explort()功能進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06