Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image JPG and jpg

I've been bothered by this problem for a while now. I'm trying to load images in PHP but there are jpg and JPG images and when I try to load images by jpg the JPG ones are not found (obviously)

$img1 = '<img src="imgs/'.$firstImage.'.jpg"></img>';
$img2 = '<img src="imgs/'.$secondImage.'.jpg"></img>';
$img3 = '<img src="imgs/'.$thirdImage.'.jpg"></img>';
$img4 = '<img src="imgs/'.$fourthImae.'.jpg"></img>';

Any idea how to fix this problem?

I tried using if_exists but that didn't work out well

if (file_exists('http://website_url.nl/imgs/'.$firstImage.'.jpg')) {
    $img1 = '<img src="imgs/'.$firstImage.'.jpg"></img>';
} else  {
    $img1 = '<img src="imgs/'.$firstImage.'.JPG"></img>';
}

Problem now is that it can't find the images with regular jpg. For example I try to load 23.jpg but the script tries to load 23.JPG because the file 23.jpg doesn't exist, while it does exist and is placed in the folder while 23.JPG isn't. It works with the JPG files. For example DOC_202.JPG is found because DOC_202.jpg doesn't exist so it loads the JPG one. But with the jpg ones it won't work. Any solutions?

With kind regards,


1 Answers

Just check for the file in your local directory, don't use the url

if(file_exists('imgs/'.$firstImage.'.jpg')) {

Most url wrapper don't support stat(), see http://www.php.net/manual/en/function.file-exists.php (blue note about php5 at the end) and http://www.php.net/manual/en/wrappers.php

Edit:
PS: Side note, when using windows the directory separator is a backslash '\', on linux it's the forward slash '/'. See global constant DIRECTORY_SEPARATOR if you need a global solution.

like image 162
clemens321 Avatar answered Dec 07 '25 19:12

clemens321



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!