/**
* @description: 删除文件夹
* @param: $dirPath=文件夹地址
* @return {*}
*/
private function deleteDirectory($dirPath) {
//防止删除错路径,检查是否是指定的目录,不需要可删除
if(strpos($dirPath,'resources/iconfont') === false){
return false;
}
// 检查目录是否存在
if (!file_exists($dirPath)) {
return false;
}
// 检查是否为目录
if (!is_dir($dirPath)) {
return unlink($dirPath); // 如果是文件,则直接删除
}
// 打开目录
$dh = opendir($dirPath);
while (($file = readdir($dh)) !== false) {
// 跳过当前目录(.)和上级目录(..)
if ($file === '.' || $file === '..') {
continue;
}
// 构建完整的文件/目录路径
$fullPath = $dirPath . '/' . $file;
// 如果是目录,则递归删除
if (is_dir($fullPath)) {
$this->deleteDirectory($fullPath);
} else {
// 如果是文件,则删除文件
unlink($fullPath);
}
}
// 关闭目录句柄
closedir($dh);
// 删除目录本身
return rmdir($dirPath);
}
php删除文件夹
于 2024-12-09 14:08:54 首次发布