Php deletes all folders and files in a path, and php deletes the path folder.
Php traverses all the files and folders in a folder, deletes the code of all the files in all the folders and subfolders, and uses recursion to clear a directory. The code is simple and practical.
It is also suitable for clearing the cache in thinkphp. In thinkphp, you can write the following code into the./Application/Admin/Common/function. php file, and then call this function in the Controller to clean up.
Functions used:
Scandir ($ path) traverses all files in a folder and returns an array.
Unlink ($ filename) deletes a file.
Rmdir ($ path) only deletes empty folders
<? Php // set the folder to be deleted $ path = ". /Application/Runtime/"; // The delete folder function and the delete folder function) {// if the directory is used, continue if (is_dir ($ path) {// scan all the folders and files in a folder and return the array $ p = scandir ($ path ); foreach ($ p as $ val) {// exclude. and .. if ($ val! = "." & $ Val! = ".. ") {// Recursive Sub-directory if it is a directory, continue to operate if (is_dir ($ path. $ val) {// Delete the folder and file deldir ($ path. $ val. '/'); // Delete the empty folder @ rmdir ($ path. $ val. '/');} else {// Delete unlink ($ path. $ val) ;}}}// call the function and input the path deldir ($ path );
Summary
The above section describes how to delete all folders and files under a path in php. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!