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

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

    This fuction delete the directory and all subdirectories and files:

    function DelDir($target) {
        if(is_dir($target)) {
            $files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned
    
            foreach( $files as $file )
            {
                DelDir( $file );      
            }
    
            rmdir( $target );
        } elseif(is_file($target)) {
            unlink( $target );  
        }
    }
    

提交回复
热议问题