1

I have a image named Dark-Green.jpg but the output of function is DARK-GREEN.jpg so the image is not displaying due to case-sensitive.

So how can I fetch the image?

UPDATE

Below is my output of the array.

$output = Array
(
  [WE05-5040*L] => Array
    (
        [qty] => 1
        [stitching_category] => 2
        [sku_image] => skuimages/WE05/DARK-GREEN.jpg
    )

)

Then I am using this array in foreach loop like below.

foreach ($output as $ok => $op) {
    $itemQty = $op['qty'];
    $itemImagePath = $op['sku_image'];
    echo "{$ok} has qty: {$itemQty} and the image as below.";
    echo "<img src='{$itemImagePath}' width='50%' />"
}
Jigar Dhaduk
  • 652
  • 1
  • 5
  • 23
  • Just stick your arm deep into whatever the image is stuck in and wiggle it out. What I really mean is: Fetch from where, how? Show us your code. – KIKO Software Aug 07 '19 at 09:46
  • @KIKOSoftware, the code is simple. I am using the output of the function to fetch the image from the folder. – Jigar Dhaduk Aug 07 '19 at 09:48
  • So it is fetching from the file-system, not from a database. Can you show us this simple code, and perhaps the function in question? – KIKO Software Aug 07 '19 at 09:49
  • had to retract vote, but pos dupe of https://stackoverflow.com/questions/3964793/php-case-insensitive-version-of-file-exists – treyBake Aug 07 '19 at 09:50
  • @KIKOSoftware. Please take a look at the updated question. – Jigar Dhaduk Aug 07 '19 at 09:58
  • @treyBake. Tried but in case-insensitive, it returns false. – Jigar Dhaduk Aug 07 '19 at 10:00
  • Thank you. So it is, in the end, a HTML link to an image, that does the fetching. Basically your output `DARK-GREEN.jpg` has lost information, namely the letter case of the image name. I see two possible solutions: 1. You reconstruct the information, for instance if all image names are camel-case you can do that. 2. Rename the image file. The best thing would, of course, be to not loose the information in the first place (if it ever existed). – KIKO Software Aug 07 '19 at 10:04
  • 1: Not all images names are in camel-case. It would be in any case. 2: The images are uploaded by the frontend user so can't rename the image. Is there any solution by which I can do with my output without changing the image name and the function output? – Jigar Dhaduk Aug 07 '19 at 10:09
  • Yes, there is, and it was already given by treyBake: You scan the folder, with `glob()` for image names similar to `DARK-GREEN.jpg`, while disregarding case, with `preg_grep()`, and use the first one of whatever comes out. This does however mean you will scan a whole directory for each HTML image link, and that's not very efficient (if there are much images in that directory). – KIKO Software Aug 07 '19 at 10:34
  • Yeah, for now, this is the workaround. – Jigar Dhaduk Aug 07 '19 at 10:45

1 Answers1

0

Try this:

function getFile ($filename){
    $files = glob($dir . '/*');
    $filename = strtolower($filename);
    foreach($files as $file) {
       if (strtolower($file) == $filename){
            return $file;
        }
     }
     return  false;
}
Ezequiel Fernandez
  • 954
  • 11
  • 18