首頁  >  文章  >  後端開發  >  php怎麼刪除全部檔案內容

php怎麼刪除全部檔案內容

藏色散人
藏色散人原創
2021-06-10 09:27:012203瀏覽

php刪除全部檔案內容的方法:先建立一個PHP範例檔案;然後透過「public function deldir($dir) {...}」方法刪除資料夾及資料夾下所有的檔案即可。

php怎麼刪除全部檔案內容

本文操作環境:windows7系統、PHP7.1版,DELL G3電腦

php怎麼刪除全部檔案內容?

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怎麼刪除全部檔案內容的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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