function delfile($filename)
{
if (!file_exists($filename)) {
return;
}
if (!is_dir($filename)) {
unlink($filename);
return;
}
if (($handle = opendir($filename))) {
while (($item = readdir($handle))) {
if ($item != "." && $item != "..") {
$fullpath = $filename . '/' . $item;
if (!file_exists($fullpath)) {
continue;
}
if (is_dir($fullpath)) {
delfile($fullpath);
rmdir($fullpath);
} else {
unlink($fullpath);
}
}
}
closedir($handle);
}
}
![](https://img2024.cnblogs.com/blog/35695/202403/35695-20240328092537879-1189167464.jpg)