-2

I'd like to scan a directory with a php function in order to find out if a file corresponding to a specific filename in database exists or not. When the file doesn't exist as a database entry (is deleted from the DB) the corresponding file should also be deleted from the directory. My problem is: scandir() doesn't only return files. Basically i need the can function in order to use its return value in a foreach.

Mondblut
  • 199
  • 1
  • 14

1 Answers1

0

You can use scandir too, of course. I'd suggest using RecursiveIteratorIterator. with RecursiveDirectoryIterator

Example code:

function recursiveDirectoryIterator($path) {
  foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $file) {
    if (!$file->isDir()) {
      yield $file->getFilename(); // You can add $file->getFileExtension() too
    }
  }
}

Normally, I'd just leave this as a comment, but I feel most people don't use RecursiveIterator and DirecotryIterator very often, so an example was in order.

Andrei
  • 3,434
  • 5
  • 21
  • 44