[英]remove empty directory in php
Hi when I would like to remove empty directory I got the following error: 嗨,当我想删除空目录时,出现以下错误:
Warning: rmdir(): Directory not empty
警告:rmdir():目录不为空
function : 功能:
if (is_dir($target)) {
$files = glob($target . '*', GLOB_MARK); //GLOB_MARK adds a slash to directories returned
foreach ($files as $file) {
$this->delete_files($file);
}
rmdir($target);
} elseif (is_file($target)) {
unlink($target);
} else {
$this->logger->error("Could not delete the folder");
}
and when there are file it deletes all as it should without warning (should delete also the folder) 并且当有文件时,它会不加警告地删除所有文件(应同时删除文件夹)
Try something like this. 尝试这样的事情。
<?php
function delete_directory($target) {
if (is_dir($target))
$dir_handle = opendir($target);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
else
delete_directory($target.'/'.$file);
}
}
closedir($dir_handle);
rmdir($target);
return true;
}
?>
Hope this helps. 希望这可以帮助。
Either you can this. 您可以这样做。
function delTree($dir)
{
$files = glob( $dir . '*', GLOB_MARK );
foreach( $files as $file
{
if( is_dir( $file ) )
delTree( $file );
else
@unlink( $file );
}
if( is_dir($dir) ) rmdir( $dir );
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.