Removing the __MACOSX folder with PHP?
Does anyone have experience with deleting a folder __MACOSX
from PHP?
The folder was created after I unpacked the archive, but I cannot delete it.
The function is_dir
returns false to the file, which causes the recursive delete scripts to fail (because there are temp files inside the archive), so the directory is not empty.
I am using the built-in ZipArchive class (extractTo method) in PHP5.
rmdir script I am using 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 );
}
?>
+2
source to share
2 answers
I found an improved version of the function from http://www.php.net/rmdir that requires PHP5.
- This function uses
DIRECTORY_SEPARATOR
instead/
. PHP detectsDIRECTORY_SEPARATOR
as the correct character for the current OS ('/' or '\'). - The directory location does not have to end with a forward slash.
- The function returns
true
orfalse
upon 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);
}
+5
source to share