PHP刪除符合條件的整個目錄,符合條件整個目錄
<?php
/**
* @name delFile函數與delDir函數一起使用, 刪除符合條件的整個目錄
* @param string $path 指定操作路徑
* @return null
* @example delDir('D:\web\Apache\htdocs\KeyShareMall\Pc\ThinkPHP');
*/
// 刪除目錄
function delFile($path)
{
if (empty($path)) {
echo '請指定要操作的文件路徑';
return false;
}
if ( $handle = opendir ( $path )) {
while ( false !== ( $fileName = readdir ( $handle ))) {
if ( $fileName != "." && $fileName != ".." ) {
if (is_file($path . '/' . $fileName)) {
unlink($path . '/' . $fileName);
}
if (is_dir($path . '/' . $fileName)) {
delFile($path . '/' . $fileName);
}
}
}
rmdir($path);
closedir ( $handle );
}
}
function delDir($path = '')
{
if (empty($path)) {
echo '請指定要操作的文件路徑';
return false;
} else {
$path = str_replace('\\', '/', $path);
}
if ( $handle = opendir($path)) {
while (false !== ( $fileName = readdir ( $handle ))) {
if ( $fileName != "." && $fileName != ".." ) {
if (is_dir($path . '/' . $fileName)) {
echo $fileName . "<br />";
// 刪除含有Zip字符的目錄
if (strpos($fileName, 'Zip') !== false) {
delFile($path . '/' . $fileName);
} else {
delDir($path . '/' . $fileName);
}
}
}
}
closedir ( $handle );
}
}
delDir('D:\web\Apache\htdocs\KeyShareMall\Pc\ThinkPHP');
?>