Created
July 6, 2013 08:31
-
-
Save mihai-vlc/5939250 to your computer and use it in GitHub Desktop.
PHP: delete all files/folders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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