user2014429
user2014429

Reputation: 2577

compare folder content with mysql data

I want to check if there are any filenames in this folder which are not in the mysql database, but I cant seem to find a way to do this as the loop for fetching the mysql data has to be inside the loop for reading the directory (as far as I can figure),which messes the whole proccess up. Is there another way to do this:

if ($handle = opendir('video/test')) {

    while (false !== ($entry = readdir($handle))) {
       if ($entry !== "." && $entry !== ".."){

  $delfile =  "SELECT * FROM files WHERE filename <> '$entry'";
  $delfile = $db->query($delfile);

  while($row = $delfile->fetch_assoc()){
         echo $row['filename'];

      }
    }
  }
   closedir($handle);
}

Upvotes: 0

Views: 763

Answers (1)

Galen
Galen

Reputation: 30170

you can use glob to get an array of filenames

$filenames = glob( 'video/test' );

then use MySQL's in to check the table

$sql = sprintf( 'select * from files where filename not in ("%s")', implode( '","', $filenames ) );

no loops!

Upvotes: 1

Related Questions