Tags
Asked 3 years ago
30 Jun 2021
Views 231
Terrill

Terrill posted

Deleting all files from a folder using PHP?

Deleting all files from a folder using PHP?
css-learner

css-learner
answered May 4 '23 00:00

To delete all files from a folder in PHP, we can use the glob() function to get a list of all the files in the folder , and then use a loop to delete each file. Here's an example:


$folder = 'path/to/folder/*';
foreach(glob($folder) as $file) {
    if(is_file($file)) {
        unlink($file);
    }
}
echo "All files deleted successfully.";

In the example above, we first define the path to the folder that contains the files we want to delete. We then use the glob() function to get a list of all the files in the folder. We loop through each file and use the unlink() function to delete the file if it is a file (not a directory). Finally, we output a success message.
Post Answer