PHP: Simplest way to delete a folder (including its contents)

后端 未结 7 2032
刺人心
刺人心 2020-12-08 14:00

The rmdir() function fails if the folder contains any files. I can loop through all of the the files in the directory with something like this:



        
7条回答
  •  死守一世寂寞
    2020-12-08 14:34

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

提交回复
热议问题