Tuesday, December 29, 2009

PHP: how delete folder with subfolders

Sometimes need delete whole directory with subfolders and files (Example cache). Standart function rmdir can delete only empty folder. I provide for you simple but powerfull function, which make your life much easier.

function _rmdir($dir) {
    if (file_exists($dir)){
    $files = glob( $dir . '*', GLOB_MARK );
    foreach( $files as $file ){
        if( substr( $file, -1 ) == '/' )
            _rmdir( $file );
        else
            unlink( $file );
    }

    if (is_dir($dir)) rmdir( $dir );
    }


code at github.com 

No comments:

Post a Comment

 
Software