Has anyone had any experience with deleting the __MACOSX folder with PHP?
The folder was generated after I unzipped an archive, but I can't seem to do delete it.
The is_dir function returns false on the file, making the recursive delete scripts fail (because inside the archive is the 'temp' files) so the directory isn't empty.
I'm using the built-in ZipArchive class (extractTo method) in PHP5.
The rmdir script I'm using is one I found on php.net:
<?php
// ensure $dir ends with a slash
function delTree($dir) {
$files = glob( $dir . '*', GLOB_MARK );
foreach( $files as $file ){
if( substr( $file, -1 ) == '/' )
delTree( $file );
else
unlink( $file );
}
rmdir( $dir );
}
?>
I found an improved version of the function from http://www.php.net/rmdir that requires PHP5.
DIRECTORY_SEPARATOR instead of /. PHP defines DIRECTORY_SEPARATOR as the proper character for the running OS ('/' or '\').true or false on completion.function deleteDirectory($dir) { if (!file_exists($dir)) return true; if (!is_dir($dir)) return unlink($dir); foreach (scandir($dir) as $item) { if ($item == '.' || $item == '..') continue; if (!deleteDirectory($dir.DIRECTORY_SEPARATOR.$item)) return false; } return rmdir($dir); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With