Tag Archives: delete

PHP : How to remove a folder and its content?

If you are using PHP then you can use this function.

/*

@params

$dir – the full directory path of the FOLDER you want to delete

$DeleteMe – if you want to delete the said FOLDER or not.

*/

function rmdir_r ( $dir, $DeleteMe = TRUE ){

if ( ! $dh = @opendir ( $dir ) ) return;
while ( false !== ( $obj = readdir ( $dh ) ) )
{
if ( $obj == ‘.’ || $obj == ‘..’) continue;
if ( ! @unlink ( $dir . ‘/’ . $obj ) ) $this->rmdir_r ( $dir . ‘/’ . $obj, true );
}

closedir ( $dh );
if ( $DeleteMe )
{
@rmdir ( $dir );
}

}