Hi when I would like to remove empty directory I got the following error:
Warning: rmdir(): Directory not empty
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 );
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.