PHP deletes the specified folder 1, preface
Target: PHP Deletes a specified directory
PHP functions used: Is_dir,opendir,readdir,scandir,rmdir,closedir, et cetera (note: Other file manipulation functions can also be completed, here only the function used this time)
2. Introduction of related functions
PHP file Operation method is basically the same, has been introduced in the previous article, here is not repeated introduction, here is introduced a new function
RmDir
Detailed reference: http://www.w3school.com.cn/php/func_filesystem_rmdir.asp
Other functions please refer to previous article: http://www.cnblogs.com/ImCehnyx/p/7214598.html
3. Code composition
** * [Delallfile description] Delete the specified directory * @param [Type]$dirName[Description] Directory * @param [Type]$file[Description] Documented process files * @paramInteger $type[Description] The first time the new file is opened by default, the second call does not open * @return [Type] [Description] */public function Delallfile ($dirName, $file, $type = 1) {$re = $this->checkdir ($dirName);/default open file for the first timeIf($type){$file=fopen($file, ' W ');Open log File}If($re){$this-Delfile($dirName,$file);}Else{Fwrite($file,"{$dirName} cleanup complete \ r \ n");}}/** * [Checkdir description] detects if the folder is empty and is empty directly delete * @param [type] $dirName [description] Folder name * @return [Type] [description] * /Private functionCheckdir($dirName) {$a=Scandir($dirName);List the files and directories in the images directory:Contains only. and.. The folder for the directory is an empty folderIf(sizeof($a) == 2){RmDir($dirName);Return False;}Return True;}/** * [delfile description] How to upload a file * @param [type] $dirName [description] directory * @param [type] $file [description] recorded files * @return [Type] [description] */Private functionDelfile($dirName,$file) {$dh=Opendir($dirName);While($handle=Readdir($dh)){If($handle== ‘.‘ ||$handle== ‘..‘) {Continue;}$resource=$dirName.‘/‘.$handle;Determine if it is a folderIf(Is_dir($resource)) {Determine if the folder is empty$re=$this-Checkdir($dirName);If($re){$this-Delallfile($resource,$file, 0);}Else{Fwrite($file,"{$resource} cleanup complete \ r \ n");Continue}}else {unlink ( $resource }}//turn off resources, delete directories closedir ( $dh Close the directory resource fwrite ( $file "{$dirName} cleanup complete \ r \ n" Rmdir ( $dirName Delete Empty directory However, when the above code encounters a sub-directory level high, it is prone to a warning that the directory cannot be deleted
Therefore, for a high-level subset, you need to add a loop at the end of the judgment, to determine whether it can be deleted, to avoid this error
Turn off resources, delete directoriesClosedir ($DH);//Close Directory ResourceFwrite ($file, "{$dirName} cleanup completed \ r \ n");RmDir ($dirName);//Delete Empty directoryEvery time the directory level is high, the error, so add a loop processing$i= 1;While($i){$re=$this-checkdir ( $dirName "$dirName $i times <br/>; $i ++; if (! $re $i = 0 Closedir ( $dh Close the directory resource Fwrite ( $file "{$dirName} cleanup complete \ r \ n" }} /span>
Add this layer can avoid the situation cannot delete (sometimes delete is the question of permission)
4. Conclusion
Deleting a directory is relatively straightforward and the basic steps are:
1, judge whether is the directory, is the directory is recursive call themselves, or delete files directly;
2, determine whether the directory is empty, not empty rmdir will throw an error;
3, the problem of file permissions (window, this is not a problem)
PHP deletes the specified folder