2012-03-12 4 views
0

내 디렉토리 구조는 다음과 같습니다.PHP에서 썸네일이 있는지 확인

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

우리가 photo-album1/ 내부 image1.jpg이 있다고 할 수 있습니다. 이 파일의 축소판은 tn_image1.jpg

입니다. 에 미리보기 이미지가있는 경우 photo-album1/의 모든 파일을 검사하고 싶습니다. 계속하지 못하면 계속해서 다른 기능에 파일 이름을 보내십시오. generateThumb()

어떻게하면됩니까? 기초 위 영혼

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

제공 :

+0

글로브(), foreach는(), file_exists() [? 당신이 시도 무엇] –

+4

을 확인하는 (http://mattgemmell.com/2008/ – j08691

+0

@ j08691 여기에 게시하는 스크립트가 매우 긴 – heron

답변

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

} 
+0

그게 뭐야? 롤. 나는 file_exists() 함수를 안다. 문제는 부모 디렉토리에서 파일을 하나씩 가져 오는 방법과 섬네일 디렉토리에서 thum을 확인하는 방법을 알아낼 수 없다는 것입니다. – heron

+0

foreach 루프를 사용 하시겠습니까? 및 게시물의 UR 코드? 그것없이 rofl도 해결책을 줄 수 없다 – Ghostman

0

쉬운 방법은 PHP의 glob 기능을 사용하는 것입니다. 그냥 glob을 추가하는 것뿐입니다.

EDIT : hakre가 지적했듯이 glob은 기존 파일 만 반환하므로 파일 이름이 배열에 있는지 확인하여 속도를 향상시킬 수 있습니다. 뭔가 같은 : 루프 디렉토리 목록을 얻을 수

if (in_array($file, $files)) echo "File exists."; 
+0

yo welcome ... :) – Ghostman

+1

흠, glob가 기존 파일 만 리턴하지 않습니까? ;) – hakre

+0

하, 좋은 지적. :) – Jemaclus

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