Deleting all files from a folder using php

Mar 12, 2012   //   by   //   Blog, PHP Tutorial  //  No Comments

I had a folder called `Temp’ and i wanted to delete or flush all files from this folder using php, could i do this?

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

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

$files = glob('path/to/temp/{,.}*', GLOB_BRACE);

Leave a comment

Share This Post

RSS Wordpress News