Post the code first:
delFile('/home/sources');
?>
Let’s first explain a few functions:
opendir(): Function opens a directory handle and can be used by closedir(), readdir() and rewinddir().
If successful, this function returns a directory stream, otherwise it returns false and an error. You can hide error output by prepending "@" to the function name. For example $dir=@ opendir("image");
readdir(): Returns the entries in the directory handle opened by the opendir function, that is, the file names in the folder are returned in order, in accordance with the sorting method specified in the file system.
id_dir(): Check whether the parameter file is a directory, and return true if so.
un_link(): Delete the specified file.
So the program execution idea is: the function calls the main directory, and then sequentially checks whether each file is a directory. If it is a directory, the function is called recursively, and the files that are not directories are deleted until all files are traversed.
This program only deletes the contents of the folder, but not the folder itself. If you want to achieve this purpose, just add the following code: