Is there a way in PHP to remove a non empty directory(folder)?
rmdir($dir); does not remove empty directory.
Unfortunately, there is not any functionality in PHP to allow you to delete non-empty directories. However, there’s a handful of examples how to recursively delete files and sub-directories using combination of ulink() and rmdir(), as @mudshark already said.
function deleteDir($dir)
{
if (substr($dir, strlen($dir)-1, 1) != '/')
$dir .= '/';
echo $dir;
if ($handle = opendir($dir))
{
while ($obj = readdir($handle))
{
if ($obj != '.' && $obj != '..')
{
if (is_dir($dir.$obj))
{
if (!deleteDir($dir.$obj))
return false;
}
elseif (is_file($dir.$obj))
{
if (!unlink($dir.$obj))
return false;
}
}
}
closedir($handle);
if (!@rmdir($dir))
return false;
return true;
}
return false;
}