Delete all the files inside folder:PHP

Did you ever come across a situation where you want to delete all the file with a particular extension from the folder?

The following code should be used To delete all the files present inside the folder:
//Here I want to delete all the word documents inside download folder.

<?php
$mypath=”public_html/download”; //replace it with your file path.
$unlink_files = glob($mypath.’*.doc’); // get all file names
foreach($unlink_files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}

?>

 

I use this script to delete error_log file.

I know its not a right thing to delete the error_log file but sometimes it is needed 😉

Leave a Reply