Archive

Posts Tagged ‘php recursively deletion dirs’

php recursively delete directorys

August 24th, 2010 No comments

When using utf8 or unicode charsets,you have the chance to find messy codes with your file or directory descriptor.And the worse,you’ll find that you cann’t delete files that has a messy code file descriptor.If you have root access to your server,just use rm -rf to delete them,but what would you do if you just have ftp access to it?Here goes the solution for you.
<?php
function _imagecache_recursive_delete($dir) {
$d = dir($dir);
if (is_dir($dir) && !is_link($dir)) {
if ($d = opendir($dir)) {
while (($entry = readdir($d)) !== false) {
if ($entry == ‘.’ || $entry == ‘..’) continue;
$path = $dir .’/’. $entry;
if (is_file($path)) unlink($path);
if (is_dir($path)) _imagecache_recursive_delete($path);
}
closedir($d);
}
return rmdir($dir);
}
return unlink($dir);
}
$dir=’./wordpress/wp-content/uploads/2010/03/’;
_imagecache_recursive_delete($dir);
?>