Skip to content

Instantly share code, notes, and snippets.

@Cojad
Last active August 29, 2015 13:58
Show Gist options
  • Save Cojad/9972608 to your computer and use it in GitHub Desktop.
Save Cojad/9972608 to your computer and use it in GitHub Desktop.
A simple function to delete a folder recursively. Very danger! Watch out!
<?php
// A simple function to delete a folder recursively. Very danger! Watch out!
// PHP用遞迴把資料夾給砍掉,殺傷力強大,請謹慎使用。
// V1.1 use readdir() instead of glob() for better non-english file name support in php4
// source unknown, modified by Jason Chiang
// xcojad at gmail.com
$folder_name="PEAR";
$folder_name="upload";
function delTree($dir) {
if ($handle = opendir($dir)) {
/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".."){
if(is_dir("$dir/$entry")){
delTree("$dir/$entry");
}
else {
unlink("$dir/$entry");
echo "Deleting File: $dir/$entry\n";
}
if(is_dir("$dir/$entry")){
echo "Deleting Dir : $dir/$entry/\n";
rmdir("$dir/$entry");
}
}
}
closedir($handle);
echo "Deleting Dir : $dir/\n";
rmdir("$dir");
}
}
delTree($folder_name);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment