凤眠

先行后知,知行合一

导航

删除一个文件或清空一个文件夹

Posted on 2015-04-11 15:54  凤眠  阅读(196)  评论(0编辑  收藏  举报
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);
}
}