Php deletes all folders and files in a path, and php deletes the path folder.

Source: Internet
Author: User

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 );


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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.