首頁  >  文章  >  後端開發  >  php如何刪除所有文件

php如何刪除所有文件

藏色散人
藏色散人原創
2020-12-02 10:04:562433瀏覽

php刪除所有檔案的方法:1、透過「deldir($fullpath);」方法刪除資料夾中所包含的所有檔案;2、透過「public function deldir($dir){...} ”方法刪除資料夾及資料夾下所有的檔案。

php如何刪除所有文件

推薦:《PHP影片教學

本教學操作環境:Windows7系統、PHP7.1版,此方法適用於所有品牌電腦。

PHP刪除資料夾及資料夾下的所有檔案

一.只刪除資料夾包含的檔案,不刪除資料夾

public function deldir($dir) {
//先删除目录下的文件:
$dh = opendir($dir);
while ($file = readdir($dh)) {
if($file != "." && $file!="..") {
$fullpath = $dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
deldir($fullpath);
}
}
}
closedir($dh);
}

二.刪除資料夾及資料夾下所有的檔案

public function deldir($dir) {
//先删除目录下的文件:
$dh = opendir($dir);
while ($file = readdir($dh)) {
if($file != "." && $file!="..") {
$fullpath = $dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
deldir($fullpath);
}
}
}
closedir($dh);
//删除当前文件夹:
if(rmdir($dir)) {
return true;
} else {
return false;
}
}

三.建立資料夾並指定權限和編碼

if (!is_dir($dir)){                                 //如果目录不存在
mkdir(iconv("UTF-8", "GBK", $dir),0777,true);   //创建目录,777权限,GBK编码格式
}

以上是php如何刪除所有文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn