Skip to content

Instantly share code, notes, and snippets.

@mihai-vlc
Created July 6, 2013 08:31
Show Gist options
  • Save mihai-vlc/5939250 to your computer and use it in GitHub Desktop.
Save mihai-vlc/5939250 to your computer and use it in GitHub Desktop.
PHP: delete all files/folders
<?php
function deleteAll($directory, $empty = false) {
$t= time() - 60*60*24*4;
if(substr($directory,-1) == "/") {
$directory = substr($directory,0,-1);
}
if(!file_exists($directory) || !is_dir($directory)) {
return false;
} elseif(!is_readable($directory)) {
return false;
} else {
$directoryHandle = opendir($directory);
while ($contents = readdir($directoryHandle)) {
if($contents != '.' && $contents != '..') {
if(filemtime($directory . "/" . $contents) < $t) {
$path = $directory . "/" . $contents;
if(is_dir($path)) {
@deleteAll($path);
} else {
@unlink($path);
}
}
}
}
closedir($directoryHandle);
if($empty == false) {
if(!@rmdir($directory)) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment