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: