如何用PHP遍曆檔案數目 或刪除目錄下的全部檔案?

來源:互聯網
上載者:User

標籤:document   刪除檔案   遍曆目錄   ase   複製   .class   讀取   lin   copy   

先說一下基礎知識:

檔案位置如所示:

1、判斷是檔案還是目錄

var_dump(filetype("./aa/bb/cc.txt"));

  輸出: string(4) "file" 

var_dump(filetype("./aa")); 

    輸出: string(3) "dir"

 2、判斷是否是檔案(是返回ture)

var_dump(is_file("./aa"));  

     輸出bool(false)

3、判斷是否是目錄(是返回ture)

var_dump(is_dir("./aa"));

  輸出: bool(true)

4、檔案的訪問、建立與修改時間

echo date("Y-m-d H:i:s",fileatime("./aa")); //檔案的上次訪問時間filectime("./aa.txt"); //檔案的建立時間echo date("Y-m-d H:i:s",filemtime("./aa.txt")); //檔案的修改時間

 5、擷取檔案大小

filesize("./aa.txt"); 

  6、判斷檔案是否存在

file_exists("./aa.txt")

  7、伺服器的根目錄

echo $_SERVER[‘DOCUMENT_ROOT‘];

  輸出:D:/phpStudy/WWW

注意:/代表根,在網頁裡面代表www目錄,在PHP裡面代表磁碟根

8、路徑

echo basename("./aa/bb/cc.txt"); //擷取路徑中的檔案名稱echo dirname("../0508/DB.class.php"); //擷取路徑中的檔案夾目錄var_dump(pathinfo("../0508/DB.class.php")); //擷取路徑資訊echo realpath("./aa/bb/cc.txt"); //將相對路徑轉化成絕對路徑

  依次輸出:

cc.txt

../0508

array(4) { ["dirname"]=> string(7) "../0508" ["basename"]=> string(12) "DB.class.php" ["extension"]=> string(3) "php" ["filename"]=> string(8) "DB.class"}
D:\phpStudy\WWW\2017-05\0519\aa\bb\cc.txt

9.目錄操作

mkdir("./aa"); //建立目錄rmdir("./aa"); //刪除目錄,目錄必須為空白rename("./test","../ceshi"); //移動目錄

  第一種遍曆目錄:

var_dump(glob("./aa/bb/*.txt")); //擷取目錄下所有檔案

  輸出:

array(6) {  [0]=>  string(14) "./aa/bb/cc.txt"  [1]=>  string(14) "./aa/bb/dd.txt"  [2]=>  string(14) "./aa/bb/ee.txt"  [3]=>  string(14) "./aa/bb/ff.txt"  [4]=>  string(14) "./aa/bb/gg.txt"  [5]=>  string(14) "./aa/bb/hh.txt"}

第二種遍曆目錄:(重要)

//開啟目錄,返回目錄資源$dname = "./aa/bb";$dir = opendir($dname);//從目錄資源裡面讀檔案,每次讀一個while($fname = readdir($dir)){echo $dname."/".$fname."<br>";}//關閉目錄資源closedir($dir);

  

輸出:

./aa/bb/.
./aa/bb/..
./aa/bb/cc.txt
./aa/bb/dd.txt
./aa/bb/ee.txt
./aa/bb/ff.txt
./aa/bb/gg.txt
./aa/bb/hh.txt

10、檔案整體操作

touch("./aa.txt"); //建立檔案copy("./aa.txt","../aa.txt"); //複製檔案unlink("./aa.txt"); //刪除檔案

11、檔案內容操作

echo file_get_contents("http://www.baidu.com"); //讀取檔案file_put_contents("./aa/bb/hh.txt","hello"); //寫內容readfile("./11.txt"); //讀取並輸出var_dump(file("11.txt")); //讀取檔案內容,返回數組,每行是一個元素

  

//開啟檔案$f = fopen("./11.txt","a");//開啟檔案並寫入fwrite($f,"wwwww");//關閉檔案fclose($f);

  其中:r唯讀;r+讀寫;w寫清空;w+讀寫;a寫入檔案末尾;a+讀寫;x建立並以寫入開啟;x+建立並以讀寫開啟;加一個b代表可操作二進位檔案(建議加)

 

利用遍曆來計算檔案夾下所有的個數

<?phpecho fileCount("./qiyezhan");function fileCount($fname){//該檔案夾下所有檔案數量$sum = 0;//判斷給的是不是檔案夾if(is_dir($fname)){//開啟檔案夾$dir = opendir($fname);while($wenjian = readdir($dir)){if($wenjian!="." &&$wenjian!=".."){//檔案的完整路徑$furl = $fname."/".$wenjian;if(is_file($furl)){$sum++;}else if(is_dir($furl)){//擷取該檔案夾下檔案數量,累加$sum = $sum + fileCount($furl);}}}//關閉檔案夾closedir($dir);return $sum;}else{echo "給的檔案夾不對";}}?>

  利用遍曆刪除全部檔案

<?phpShanChu("./qiyezhan");function ShanChu($fname){if(is_dir($fname)){//在刪除之前,把裡面的檔案全部刪掉$dir = opendir($fname);while($dname = readdir($dir)){                         //必須加這一項,不然可能會將整個磁碟給刪掉if($dname!="." && $dname!=".."){$durl = $fname."/".$dname;if(is_file($durl)){unlink($durl);}else{ShanChu($durl);}}}closedir($dir);//刪除該檔案夾rmdir($fname);}else{//如果是檔案,直接刪掉unlink($fname);}}?>

 

如何用PHP遍曆檔案數目 或刪除目錄下的全部檔案?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.