Php code for deleting folders and files in folders-PHP source code

Source: Internet
Author: User
Unlink is used to delete files in php. If a single file is deleted, we can use unlink directly. If the file is deleted in a directory, We need to traverse the directory and perform recursive deletion. The following is an example. Unlink is used to delete files in php. If a single file is deleted, we can use unlink directly. If the file is deleted in a directory, We need to traverse the directory and perform recursive deletion. The following is an example.

Script ec (2); script

Before learning this method, we use the rmdir () function. This function deletes a file or folder Based on the specified file path. However, when a folder is deleted, when there is still content in the folder, the Directory not empty error will occur. How can this problem be solved? Now we can compile a function to solve this problem.

In this example, several important file operations functions in php are used,
1. opendir: If the function runs successfully, a group of directory streams (a group of directory strings) are returned. If the function fails, an error [error] is returned. You can add "@" at the beginning of the function to hide errors.
2. readdir: The returned result from the directory is. (This indicates the directory to be read.), return .. (indicates that the file is already in the directory and is being read from the directory) and then the name of the file or folder is returned. If not, false is returned.
3. closedir: Close the directory stream
Complete code:

The Code is as follows:





Insert title here


Function delDirAndFile ($ dirName, $ flag ){
If (@ $ handle = opendir ("$ dirName ")){
// If the function runs successfully, a set of directory streams (a set of directory strings) are returned. If the function fails, an error [error] is returned. You can add "@" at the beginning of the function to hide errors.
While (false! ==( $ Item = readdir ($ handle ))){
// The result returned from the directory is. (This indicates the directory to be read.), return .. (indicates that the file is already in the directory and is reading the file in the directory)
// Then return the name of the file or folder. If not, false is returned.
If ($ item! = "." & $ Item! = ".."){
// When there is a file or folder, start to make a judgment.
If (is_dir ("$ dirName/$ item ")){
// For folders, use them recursively
DelDirAndFile ("$ dirName/$ item ");
} Else {
// Otherwise, the object will be deleted.
If (unlink ("$ dirName/$ item "))
// Determine whether the object is successfully deleted
Echo "delete file $ dirName/$ itemn ";
}
}
}
Closedir ($ handle); // close the file directory stream
If ($ flag = true ){
If (rmdir ($ dirName ))
Echo 'Directory and File deleted successfully'; // Finally, delete the Input Folder.
}
}
}
?>

DelDirAndFile ("dd", false );
?>

The basic introduction of the Code is described in the Code. Now let's take a look at the test results:
File structure directory before execution
Result displayed on the execution page:
The structure of the price directory after execution:

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: info-contact@alibabacloud.com 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.