Php deletes a specified folder,
Php deletes a specified folder. 1. Preface
Target: php deletes a specified directory.
The php functions used: is_dir, opendir, readdir, scandir, rmdir, closedir, and so on (Note: Other file operation functions can also be completed. Only the functions used this time are listed here)
2. Related functions
Operations on PHP files are roughly the same. I have already introduced them in the previous article. I will not repeat them here. Here I will introduce a new function.
Rmdir
Http://www.w3school.com.cn/php/func_filesystem_rmdir.asp for details
For other functions, see previous: http://www.cnblogs.com/ImCehnyx/p/7214598.html
3. code structure
* ** [DelAllFile description] Delete the specified directory * @ param [type] $ dirName [description] Directory * @ param [type] $ file [description] The recorded process file * @ param integer $ type [description] the first time a 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); // the first time the file is opened by default, if ($ type) {$ file = fopen ($ file, 'w '); // open the log file} if ($ re) {$ this-> delFile ($ dirName, $ file);} else {fwrite ($ file, "{$ dirName} cleared \ r \ n");}/*** [checkDir description] Check whether the folder is empty, directly Delete * @ param [type] $ dirName [description] folder name * @ return [type] [description] */private function checkDir ($ dirName) {$ a = scandir ($ dirName); // list the files and directories in the images Directory: // only contain. and .. the folder in the directory is empty. if (sizeof ($ a) = 2) {rmdir ($ dirName); return false;} return true ;} /*** [delFile description] Method for uploading files * @ param [type] $ dirName [description] Directory * @ param [type] $ file [description] recorded files *@ return [type] [description] */private function delFile ($ dirName, $ file) {$ dh = openDir ($ dirName); while ($ handle = readdir ($ dh) {if ($ handle = '. '| $ handle = '.. ') {continue;} $ resource = $ dirName. '/'. $ handle; // determine whether it is a folder if (is_dir ($ resource) {// determine whether the folder is empty $ re = $ this-> checkDir ($ dirName ); if ($ re) {$ this-> delAllFile ($ resource, $ file, 0);} else {fwrite ($ file, "{$ resource} cleared \ r \ n"); continue ;}} else {unlink ($ resource) ;}// turn off the resource, delete the directory closedir ($ dh); // close the directory resource fwrite ($ file, "{$ dirName} cleared \ r \ n"); rmdir ($ dirName ); // delete an empty directory}However, when the code above encounters a sub-directory level high, it is prone to a warning that the directory cannot be deleted.
Therefore, for a high-level subset, we need to add a loop at the end to determine whether it can be deleted to avoid this error.
//// Close the resource and delete the directory // closedir ($ dh); // close the directory resource // fwrite ($ file, "{$ dirName} cleared \ r \ n"); // rmdir ($ dirName); // delete an empty directory // an error is returned when the directory level is high, therefore, add a loop $ I = 1; while ($ I) {$ re = $ this-> checkDir ($ dirName ); echo "$ dirName $ I Times <br/>
This layer can be used to avoid deletion failures (sometimes it is a permission issue)
4. Conclusion
It is relatively easy to delete a directory. The basic steps are as follows:
1. determine whether it is a directory. If it is a directory, recursively call itself; otherwise, delete the file directly;
2. Check whether the directory is empty. If the directory is not empty, an error is thrown;
3. File Permission issues (window, this is not a problem)