Alex Rivera | Logout

Deleting all files from a folder using PHP?

Asked 2011-01-04T13:35:43.857
374

For example I had a folder called `Temp' and I wanted to delete or flush all files from this folder using PHP. Could I do this?

Edit
Report

1 Answer

731
$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
  if(is_file($file)) {
    unlink($file); // delete file
  }
}

If you want to remove 'hidden' files like .htaccess, you have to use

$files = glob('path/to/temp/{,.}*', GLOB_BRACE);
answered 2011-01-04T13:43:54.270

Your Answer