Deleting all files of a directoryWe have seen how a file can be deleted by using unlink function in PHP. The same function can be used along with directory handler to list and delete all the files present inside. We have discussed how to display all the files present inside a directory. Now let us try to develop a function and to this function we will post directory name as parameter and the function will use unlink command to remove files by looping through all the files of the directory.
Here is the code to this.
Here images is the directory name we want to empty
Deleting Selected files through checkboxesAs we know how to display all files of a directory , we will use same concept to fist display the list of files. While displaying the file names we will add one checkbox before it for selection of file by user for deletion. For this we have used one array of checkbox.
On submit of the form we can loop through the array of checkbox to identify files which are selected by user.
Then we will select the path and delete the selected files by using unlink command.
Here is the complete code.
Marking for deletionWe can modify the above script and instead of deleting the file we can display them with strikeout text showing marked for deletion. On further confirmation from the user same can be deleted.