Skip to content

Instantly share code, notes, and snippets.

@aliva
Last active August 29, 2015 14:14
Show Gist options
  • Save aliva/42d06f26694bac7106fc to your computer and use it in GitHub Desktop.
Save aliva/42d06f26694bac7106fc to your computer and use it in GitHub Desktop.
rm -rf on php
<?php
function rmTree($path){
$pathes = array($path);
while(true){
if (count($pathes) == 0){
break;
}
$path = array_pop($pathes);
echo "$path";
if(is_dir($path)){
$files = array_diff(scandir($path), array('.','..'));
if(count($files) == 0){
rmdir($path);
}
else{
array_push($pathes, $path);
foreach($files as &$val){
$val = "$path/$val";
}
$pathes = array_merge($pathes, $files);
}
}
else{
unlink($path);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment