/**
* 删除目录及目录下所有文件或删除指定文件
* DIRECTORY_SEPARATOR 为TP5系统变量,区别Windows和Linux下的目录反斜杠
* @param string $path 待删除目录路径
* @param bool $delDir 是否删除目录,1或true删除目录,0或false则只删除文件保留目录(包含子目录)
* @return bool 返回删除状态
*/
function del_dir_and_file($path = '', $delDir = true)
{
$path = !empty($path) ? $path : think\facade\Env::get('runtime_path');
$handle = opendir($path);
if ($handle) {
while (false !== ( $item = readdir($handle) )) {
if ($item != "." && $item != "..") {
if (is_dir($path . DIRECTORY_SEPARATOR . $item)) {
@chmod($path . DIRECTORY_SEPARATOR . $item, 0777);
del_dir_and_file($path . DIRECTORY_SEPARATOR . $item, $delDir);
} else {
unlink($path . DIRECTORY_SEPARATOR . $item);
}
}
}
closedir($handle);
if ($delDir) {
@rmdir($path);
return true;
}
} else {
if (file_exists($path)) {
return unlink($path);
} else {
return false;
}
}
}