Skip to main content

php delete files in a folder and the folder

function rrmdir($dir)

{

if (is_dir($dir))  {

$objects = scandir($dir);

foreach ($objects as $object) {     if ($object != "." && $object != "..")  {  if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);       }     }     reset($objects);     rmdir($dir);   }

}

$delpath = "user_data"; // directory that has the files

rrmdir($delpath);  // deletes all files inside the directory

// Remove the directory
if(is_dir($delpath))  rmdir($delpath);

Comments