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.

Community
  • 1
  • 1
Gogol
  • 3,033
  • 4
  • 28
  • 57
  • 2
    Have you tried the [second answer](http://stackoverflow.com/a/2520643/749181) to the linked question? It suggests that you fetch the filenames first, then compare them in a case-insensitive manner in a second step. – George Cummins May 18 '15 at 13:17
  • @GeorgeCummins I have over 300000 images and 60000 products to add. I know I could do this but it would be too expensive isn't it? If nothing works, then I will have to take this approach. :) – Gogol May 18 '15 at 13:24
  • 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 '15 at 13:27
  • @Rizier123 Could you give me an example how? Sorry if this sounds noobish. I am perplexed at this moment lol. – Gogol May 18 '15 at 13:30
  • Is it an option to just fix the actual problem instead? So the files are NOT mixed case? Just a suggestion -- I find every time I code something like this, I end up having to do multiple versions of it for different aspects of the site. – DragonYen May 18 '15 at 13:40
  • @DragonYen I am trying to re-construct a 3 year old e-commerce system which grew up without following proper data structure ( and constraints, too). Being an IT guy, I am sure you must have already faced a project like this, or you will face it some day. No wonder I am having nightmares these days lol. No, it is not possible to fix it anymore. – Gogol May 18 '15 at 16:41
  • 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 '15 at 16:53
  • Hmm I guess this is what I am gonna do tomorrow. I am gonna take all the images and do a pattern matching. I will post my code nevertheless. Thanks :) – Gogol May 18 '15 at 16:55
  • but this is so bad that PHP does not have a way to do this.. Seems logical to me to keep a flag to make the whole search case insensitive irrespective of the operating system. – Gogol May 18 '15 at 16:56
  • 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. – Funk Forty Niner May 18 '15 at 19:19
  • 1
    You could also try doing a batch rename to upper/lower case; that's an option. – Funk Forty Niner May 18 '15 at 19:24

1 Answers1

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.

Gogol
  • 3,033
  • 4
  • 28
  • 57
  • 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* – Funk Forty Niner May 20 '15 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 '15 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* – Funk Forty Niner May 20 '15 at 20:37