Skip to content

Instantly share code, notes, and snippets.

@AlexH-HankIT
Created August 17, 2015 01:01
Show Gist options
  • Save AlexH-HankIT/bd837f7097d9f1a1e6db to your computer and use it in GitHub Desktop.
Save AlexH-HankIT/bd837f7097d9f1a1e6db to your computer and use it in GitHub Desktop.
Delete a directory and its content
/*****
*@dir - Directory to destroy
*@virtual[optional]- whether a virtual directory
*@link http://webdeveloperplus.com/php/21-really-useful-handy-php-code-snippets/
*/
function destroyDir($dir, $virtual = false)
{
$ds = DIRECTORY_SEPARATOR;
$dir = $virtual ? realpath($dir) : $dir;
$dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir;
if (is_dir($dir) && $handle = opendir($dir))
{
while ($file = readdir($handle))
{
if ($file == '.' || $file == '..')
{
continue;
}
elseif (is_dir($dir.$ds.$file))
{
destroyDir($dir.$ds.$file);
}
else
{
unlink($dir.$ds.$file);
}
}
closedir($handle);
rmdir($dir);
return true;
}
else
{
return false;
}
}
- See more at: http://webdeveloperplus.com/php/21-really-useful-handy-php-code-snippets/#sthash.rBXf130A.dpuf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment