Posts Tagged ‘php recursively deletion dirs’

php recursively delete directorys

August 24th, 2010 Comments off

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.
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);
return rmdir($dir);
return unlink($dir);