I was working on some Dataoperation, and just wanted to share an OOP method with you.
It just removes any contents of a Directory but not the target Directory itself! Its really nice if you want to clean a BackupDirectory or Log.
Also you can test on it if something went wrong or if it just done its Work!
I have it in a FileHandler class for example, enjoy!
<?php
public function deleteContent($path){
try{
$iterator = new DirectoryIterator($path);
foreach ( $iterator as $fileinfo ) {
if($fileinfo->isDot())continue;
if($fileinfo->isDir()){
if(deleteContent($fileinfo->getPathname()))
@rmdir($fileinfo->getPathname());
}
if($fileinfo->isFile()){
@unlink($fileinfo->getPathname());
}
}
} catch ( Exception $e ){
return false;
}
return true;
}
?>