7

I need to check the image folder for adding some product images. My product list array has SKUs such as a48be25, A48be29, A48BE30 and my image folder has images such as a48BE25_1.jpg, a48bE29_2.JPG, A48BE30_1.jpg and so on.

As you can see, the images and SKUs are mixed. I need to somehow match SKUs to the file names. If I use glob("my/dir/{$SKU}*.jpg"), it won't work in case sensitive operating systems according to the best of my knowledge. Is there a way to force glob to search in a case-insensitive way?

EDIT: I don't think this thread is a duplicate of this one. I am saying this because in my case I can have many SKUs that can have mixed cases. In the mentioned thread, OP only had the word CSV in mixed cases, so glob('my/dir/*.[cC][sS][vV]') could work well there.

11
  • 2
    Have you tried the second answer to the linked question? It suggests that you fetch the filenames first, then compare them in a case-insensitive manner in a second step. May 18, 2015 at 13:17
  • 1
    @noc2spamツ I think if you have so many images then it is faster to handle this with php and don't let the filesystem do this.
    – Rizier123
    May 18, 2015 at 13:27
  • 1
    Alright. Just seemed better to use the processing power ONCE (to loop the directory and either upper or lowercase all files) instead of having to do it on every display of a page. Good luck to you.
    – DragonYen
    May 18, 2015 at 16:53
  • 1
    If you were on a Windows system, then case wouldn't be an issue. Unfortunately, you're on Linux and it's a different animal altogether. May 18, 2015 at 19:19
  • 1
    You could also try doing a batch rename to upper/lower case; that's an option. May 18, 2015 at 19:24

1 Answer 1

3

I ultimately ended up fetching all images from the folder and checking for each sku in the image name array.

The following code solved my problem:

$path = $image_path ."/*.{jpg,png,gif}";
$all_images = glob($path, GLOB_BRACE);
$icount = count($all_images);
for($i = 0; $i < $icount; $i++)
{
    $all_images[$i] = str_replace($image_path.'/', '', $all_images[$i]);
}

foreach($products as $product){
    $matches  = preg_grep ('/^'.$product['sku'].'(\w+)/i', $all_images);
}

Nevertheless, I would love to see case-insensitive glob implemented in future.

3
  • 1
    I've been keeping this tab open lol - Glad to see you've found a solution, however a case-insensitive glob as you state, would be a worthy addition to a future PHP release. Something you can post on php.net - Cheers May 20, 2015 at 15:18
  • Sorry man I can't post the C code. I am too noob for that. I am a web dev only. I hope some one does it in a future php release. :)
    – Gogol
    May 20, 2015 at 20:36
  • 2
    No problemo. I'll pass the word around though. I've a friend who knows this guy at PHP.net ;-) cheers May 20, 2015 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.