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.";
   }
}

基本については、上記の魂の功績です。私はそれにグロブを追加しています。

編集: hakre が指摘するように、glob は既存のファイルのみを返すため、ファイル名が配列内にあるかどうかを確認するだけで高速化できます。何かのようなもの:

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