2012-10-24 4 views
0

디렉토리를 검색하고 모든 jpg 파일을 나열하는이 정렬 기능이 있는데, 파일 이름이 지정된 키워드와 일치하는 파일 만 jpg으로 정렬하려면 어떻게해야합니까? 예를 들어 "toys"이라는 이름을 가진 모든 jpg 파일을 찾고 정렬 할 수 있습니다.키워드 배열로 파일 정렬 방법 파일 이름

$a_img[] = array(); // return values 
$keyword = "toys"; // your keyword 
$allowed_types = array('jpg'); // list of filetypes you want to show 
$dimg = opendir($imgdir); 
while($imgfile = readdir($dimg)) { 
    // check to see if filename contains keyword 
    if(false!==strpos($keyword, $imgfile)){ 
     //check file extension 
     $extension = strtolower(substr($imgfile, strrpos($imgfile, ".")+1)); 
     if (in_array($extension, $allowed_types)) { 
     // add file to your array 
     $a_img[] = $imgfile; 
     } 
    } 
} 
// sort alphabetically by filename 
sort($a_img); 


    $totimg = count($a_img); // total image number 
    for($x=0; $x < $totimg; $x++) 
     { 

    $size = getimagesize($imgdir.'/'.$a_img[$x]); 
    // do whatever 

    echo $a_img[$x]; 

    } 
당신은 strpos() 사용하여 키워드의 발생에 대한 파일 이름을 확인하고 싶은 것

답변

0

- http://php.net/manual/en/function.strrpos.php을 - 오직 당신의 배열에 해당 파일을 추가 할 수 있습니다. 당신이 알파벳 순으로 파일 이름을 기준으로 정렬 할 가정하면, 당신은 sort() 기능을 사용하여이 배열을 정렬 - !==false을 테스트해야합니다 strpos()를 사용하는 경우 http://php.net/manual/en/function.sort.php

을, 그렇지 않으면 파일 이름 (예 : "toys_picture.jpg")의 시작 부분에 키워드 거짓이 아니라 거짓이 아닌 0을 반환합니다. http://www.php.net/manual/en/function.strrpos.php - - 당신의 이름에 .의 마지막 발생을 발견하고 3, 4 문자 파일 확장자 ("JPG"모두 예를 들면 "JPEG"을 지원하고자한다 substr()을 위해 그것을 사용

또한 strrpos() 사용할 수).

$imgdir = "idximages"; // directory 
$keyword = "toys"; // your keyword 
$allowed_types = array('jpg'); // list of filetypes you want to show 
$a_img = array(); // return values 
$dimg = opendir($imgdir); 
while($imgfile = readdir($dimg)) { 
    // check to see if filename contains keyword 
    if(false!==strpos($imgfile, $keyword)){ 
     //check file extension 
     $extension = strtolower(substr($imgfile, strrpos($imgfile, ".")+1)); 
     if (in_array($extension, $allowed_types)) { 
      // add file to your array 
      $a_img[] = $imgfile; 
     } 
    } 
} 
// sort alphabetically by filename 
sort($a_img); 

// iterate through filenames 
foreach ($a_img as $file){ 
    $imagesize = getimagesize($imgdir.'/'.$file); 
    print_r($imagesize); 
    list($width, $height, $type, $attr) = $imagesize; 
} 
+0

도움을 주셔서 감사합니다. 불행히도 배열과 관련하여 완전히 혼란 스럽습니다. 하지만 내 코드를 추가하여 오류가 발생하지 않았지만 결과가 반환되지 않았습니다. 각 키워드에 대해 13 개의 이미지가 있으므로 이미지 세트는 toys.jpg, toysa.jpg, toysb.jpg, toysc.jpg 및 cars.jpg, carsa.jpg, carsb.jpg라는 이미지 세트로 이름이 지정됩니다. 등등 ... 그래서 나는 키워드 장난감 그들과 함께 모든 JPG 파일을 반환 finda해야합니다. –

+0

코드가 깨지는 부분을보기 위해 일부 'echo'줄을 추가하려 했습니까? – doublesharp

+0

@KarenBemusOsh - 나쁘다.'if (false! == strpos ($ imgfile, $ keyword))'에 대한 매개 변수가 거꾸로 있으면'strpos ($ haystack, $ needle)'이어야합니다. 내 편집 내용을 확인하십시오. – doublesharp

0

사용 strrpos는 해당 페이지의 예에서 다른 문자열에 문자열의 존재를

http://php.net/manual/en/function.strrpos.php

룩을 확인하고 당신은 toys 일치 할 수 있어야하고 있다면 배열에서 해당 제외 그렇지 않다.

0

아마도 내가 놓친 것 같습니다.하지만 "예, 그것은 JPG입니다"라고 말한 후에는 다음과 같이 할 수 있습니다.

if(strstr($imgfile, 'toys')) 
{ 
    //carry on 
}