Many time when you are working with file sin php you can find this scenario: You need to erase a directory and all his content (another directories and files inside).
For doing this the easier way is using a recursive function that makes this work for us.
I tried few of them and they work, but my favourite one was found in php.net (currently the link doesn’t have the comment anymore :S)
function removeRessource($_target) {//remove the done file
//file?
if( is_file($_target) ) {
if( is_writable($_target) ) {
if( @unlink($_target) ) {
return true;
}
}
return false;
}
//dir recursive
if( is_dir($_target) ) {
if( is_writeable($_target) ) {
foreach( new DirectoryIterator($_target) as $_res ) {
if( $_res->isDot() ) {
unset($_res);
continue;
}
if( $_res->isFile() ) {
removeRessource( $_res->getPathName() );
} elseif( $_res->isDir() ) {
removeRessource( $_res->getRealPath() );
}
unset($_res);
}
if( @rmdir($_target) ) return true;
}
return false;
}
}
To use it just call the function with the given path, that’s all.