您的位置:首页 > 编程语言 > PHP开发

转载一个thinkphp 删除文件以及目录的方法

2017-08-10 11:54 555 查看
/**
+-----------------------------------------------------------------------------------------
* 删除目录及目录下所有文件或删除指定文件
+-----------------------------------------------------------------------------------------
* @param str $path 待删除目录路径
* @param int $delDir 是否删除目录,1或true删除目录,0或false则只删除文件保留目录(包含子目录)
+-----------------------------------------------------------------------------------------
* @return bool 返回删除状态
+-----------------------------------------------------------------------------------------
*/
function delDirAndFile($path, $delDir = FALSE) {
if (is_array($path)) {
foreach ($path as $subPath)
delDirAndFile($subPath, $delDir);
}
if (is_dir($path)) {
$handle = opendir($path);
if ($handle) {
while (false !== ( $item = readdir($handle) )) {
if ($item != "." && $item != "..")
is_dir("$path/$item") ? delDirAndFile("$path/$item", $delDir) : unlink("$path/$item");
}
closedir($handle);
if ($delDir)
return rmdir($path);
}
} else {
if (file_exists($path)) {
return unlink($path);
} else {
return FALSE;
}
}
clearstatcache();
}

来自:http://www.thinkphp.cn/code/138.html

路径要注意,我本地测试使用的绝对路径才成功,thinkphp3.1版本,其他版本不知道啊
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHP thinkphp