Skip to content

Instantly share code, notes, and snippets.

@paulund
Created March 29, 2013 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulund/5274044 to your computer and use it in GitHub Desktop.
Save paulund/5274044 to your computer and use it in GitHub Desktop.
Delete a directory and all files in the directory with PHP.
<?php
function delete_directory($dirname) {
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
else
delete_directory($dirname.'/'.$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment