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

后端 未结 7 2063
刺人心
刺人心 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:23

    One safe and good function located in php comments by lprent It prevents accidentally deleting contents of symbolic links directories located in current directory

    public static function delTree($dir) { 
       $files = array_diff(scandir($dir), array('.','..')); 
        foreach ($files as $file) { 
          (is_dir("$dir/$file") && !is_link($dir)) ? delTree("$dir/$file") : unlink("$dir/$file"); 
        } 
        return rmdir($dir); 
      } 
    

提交回复
热议问题