0

I have this sort function which scans a directory and lists all jpg files, how could I make it to sort only jpg files whose file-name match a specified keyword, for example to find and sort all jpg files whose name includes the keyword "toys".

   $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];  

  }
Jørgen R
  • 10,568
  • 7
  • 42
  • 59

3 Answers3

0

You would want to check the filename for occurrences of your keyword using strpos() - http://php.net/manual/en/function.strrpos.php - and only add those files to your array. Assuming you want to sort alphabetically by filename, you sort this array using the sort() function - http://php.net/manual/en/function.sort.php

When using strpos() make sure to test for !==false otherwise your keyword at the beginning of the filename (for example "toys_picture.jpg") will return 0, which is falsey but not false.

You can also use strrpos() - http://www.php.net/manual/en/function.strrpos.php - to find the last occurrence of a . in your filename and use that for substr() should you want to support 3 and 4 character file extensions (both "jpg" and "jpeg" for example).

$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;
}
doublesharp
  • 26,888
  • 6
  • 52
  • 73
  • thank you so much for your help, I am totally flustered with arrays unfortunately, i just don't get it. but I tried adding your code to mine it didn't throw an error but it didn't return any results either. I have 13 images for each keyword so a set of images are named like toys.jpg, toysa.jpg, toysb.jpg, toysc.jpg, and another set of images might be named cars.jpg, carsa.jpg, carsb.jpg, etc... so I need to finda return all jpg files with the keyword toys in them. – Karen Bemus Osh Oct 25 '12 at 00:39
  • Have you tried adding some `echo` lines to see where the code is breaking down? – doublesharp Oct 25 '12 at 00:46
  • @KarenBemusOsh - my bad, I had the parameters for `if(false!==strpos($imgfile, $keyword))` backwards, it should be `strpos($haystack, $needle)`. Please see my edits. – doublesharp Oct 25 '12 at 00:50
  • yes, I have an echo in it, but it just returns false and shows nothing. I'd like to paste my new code in here but I can't figure out where to paste it (yeah I'm totally ignorant lol) – Karen Bemus Osh Oct 25 '12 at 00:51
  • ok I tried your edit code but now I'm getting the following error Warning: sort() expects parameter 1 to be array, null given – Karen Bemus Osh Oct 25 '12 at 00:58
  • I pasted my new code in my original post, now it's putting the Array in the image directory path and returning the error Warning: getimagesize(idximages/Array) [function.getimagesize]: failed to open stream: No such file or directory – Karen Bemus Osh Oct 25 '12 at 01:06
  • you need to make sure to define your array `$a_img[] = array();` - what is the value of `$imgdir`? – doublesharp Oct 25 '12 at 01:12
  • yes, I forgot to add the $a_img[] = array(); after I added it (see the code in my original post) it then returns the error Warning: getimagesize(idximages/Array) [function.getimagesize]: failed to open stream: No such file or directory (notice it's calling the directory as idximages/Array – Karen Bemus Osh Oct 25 '12 at 01:17
  • Sorry, I actually put the code into a file to test - you need `$a_img = array();`, if you use `$a_img[] = array();` then you are setting on of the array elements to another array. See updates and it should work. – doublesharp Oct 25 '12 at 01:40
  • Great, it would be appreciated if you could mark this as the answer now that everything is working as expected. – doublesharp Oct 26 '12 at 01:13
0

Use strrpos to check for a substring's existence in another string

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

Look at the examples on that page and you should be able to match toys and exclude that from your array if it's not there

Landon
  • 4,088
  • 3
  • 28
  • 42
0

Perhaps I've missed something but after you've said "yes, it's a JPG" you'd do something like:

if(strstr($imgfile, 'toys'))
{
  //carry on
}
Martin Lyne
  • 3,157
  • 2
  • 22
  • 28