-1

Following this thread (first post) I have successfully accomplished the task of deleting all files from a folder using php.

This is the code I use:

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

I would like to exclude some files from being deleted. What code adjustment should I apply?

Community
  • 1
  • 1
AzMandius
  • 41
  • 1
  • 1
  • 5

1 Answers1

1
$files = glob('path/to/temp/*'); // get all file names
$exceptions = ["awesomefile_a", "awesomefile_b"];
foreach($files as $file){ // iterate files
  if(is_file($file) && !in_array(end(explode("/", $file)), $exceptions))
    unlink($file); // delete file
}
nettux
  • 5,270
  • 2
  • 23
  • 33