0

我想检查此文件夹中是否有任何不在 mysql 数据库中的文件名,但我似乎无法找到一种方法来执行此操作,因为获取 mysql 数据的循环必须在读取目录的循环内(据我所知),这把整个过程搞砸了。还有另一种方法可以做到这一点:

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);
}
4

1 回答 1

1

您可以使用glob获取文件名数组

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

然后使用 MySQL 的in检查表

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

没有循环!

于 2013-02-08T02:36:22.023 回答