1

I have a set of files that looks like this:

image01.png
image01.jpg
image02.png
image02.jpg
image03.png
image03.jpg
image03.gif

Can anyone think of a better way to get only one file from each set with the same basename, and based on a prioritized set of extensions? All the continue statements in my current code looks... not so good. And I am positive I will add more filetypes to the mix, so it won't get any prettier (or easier to manage for that matter).

while(($Filename = readdir($DirHandle)) !== FALSE){
    $Ext = pathinfo($Filename, PATHINFO_EXTENSION);
    $Basename = basename($Filename, '.'.$Ext);
    switch($Ext){
        case 'png':
            // highest priority, we're good
            break;

        case 'jpeg':
        case 'jpg':
            // is there a higher-priority filetype with the same basename?
            if(file_exists($Dir.'/'.$Basename.'.png'))
                continue 2; // then, let's proceed to the next file
            break;

        case 'gif':
            if(file_exists($Dir.'/'.$Basename.'.png'))
                continue 2;
            elseif(file_exists($Dir.'/'.$Basename.'.jpeg'))
                continue 2;
            elseif(file_exists($Dir.'/'.$Basename.'.jpg'))
                continue 2;
            break;

        // etc. etc...

        default:
            // not a filetype we're interested in at all
            continue 2;
    }
}

A scandir() solution would be fine too. But even then, I personally couldn't think of any better solution..

user966939
  • 692
  • 8
  • 27

2 Answers2

0

You could simply make use of PHP's SPL Extensions..

$it_files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("images",FilesystemIterator::CURRENT_AS_SELF));

$images = array();
foreach ($it_files as $file) {
    if(!array_key_exists(pathinfo($file)['filename'],$images) && exif_imagetype($file))
    {
        $images[pathinfo($file)['filename']]=pathinfo($file)['basename'];
    }
}
print_r($images);

Explanation :

The above code recursively checks all the files under the images directory. Each file is checked whether it is an image or not using exif_imagetype so don't have to specify the extensions like .jpg , .jpeg, .gif... etc.

Let us consider the same files you mentioned are there in the images directory.. First it will add image01.png to the $images array since it is an image and no other occurence was there. Now, the image01.jpg will not be added since their filenames match. Now, the image02.png will be added.. an so on.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

As I'm not only working with images (which I maybe should have stated in the first place), and specifically through extensions (for a certain reason), the solution posted by Shankar won't really work for me.

So here is what I finally came up with:

$PriExts = array('png','jpg','jpeg','gif');

// ...

if (!in_array($Ext, $PriExts)) continue;
$i = array_search($Ext, $PriExts);
while($i > 0){
    if(file_exists($Dir.'/'.$Basename.'.'.$PriExts[--$i])){
        continue 2;
    }
}
user966939
  • 692
  • 8
  • 27