0

我的目录结构看起来像这样。

 ...photo-album1/
 ...photo-album1/thumbnails/

可以说我们有image1.jpg里面photo-album1/。此文件的缩略图是tn_image1.jpg

我想做的是检查里面的每个文件photo-album1/是否有缩略图photo-album1/thumbnails/。如果他们刚刚继续,则将文件名发送到另一个函数:generateThumb()

我怎样才能做到这一点?

4

3 回答 3

3
<?php

$dir = "/path/to/photo-album1";

// Open directory, and proceed to read its contents
if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
    // Walk through directory, $file by $file
    while (($file = readdir($dh)) !== false) {
      // Make sure we're dealing with jpegs
      if (preg_match('/\.jpg$/i', $file)) {
        // don't bother processing things that already have thumbnails
        if (!file_exists($dir . "thumbnails/tn_" . $file)) {
          // your code to build a thumbnail goes here
        }
      }
    }
    // clean up after ourselves
    closedir($dh);
  }
}
于 2012-03-12T20:07:26.850 回答
1
$dir = '/my_directory_location';
$files = scandir($dir);//or use 
$files =glob($dir);
foreach($files as $ind_file){
if (file_exists($ind_file)) {
    echo "The file $filexists exists";
    } else {
    echo "The file $filexists does not exist";
    }

} 
于 2012-03-12T19:58:33.943 回答
0

简单的方法是使用 PHP 的glob函数:

$path = '../photo-album1/*.jpg';
$files = glob($path);
foreach ($files as $file) {
   if (file_exists($file)) {
      echo "File $file exists.";
   } else {
      echo "File $file does not exist.";
   }
}

归功于基础知识。我只是在其中添加 glob。

编辑:正如 hakre 所指出的,glob 只返回现有文件,因此您可以通过检查文件名是否在数组中来加快速度。就像是:

if (in_array($file, $files)) echo "File exists.";
于 2012-03-12T20:05:10.057 回答