user6223574
user6223574

Reputation:

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,

Upvotes: 3

Views: 421

Answers (2)

Per Enstr&#246;m
Per Enstr&#246;m

Reputation: 1086

The function file_exists checks on the local system wether a file exists or not, not on the external URL.

http://php.net/manual/en/function.file-exists.php

So if you change the file_exists parameter to your local address it should work. For example:

if(file_exists('/webdirectory/public_html/project/images/' . $image1 . '.jpg')){
    //do stuff
}

The case sensitiveness of file_exists depends on the file system it resides on. On unix servers file case matters, and so will file_exists.

This comment in the PHP manual suggests a way of checking via URL: http://php.net/manual/en/function.file-exists.php#75064

Upvotes: 0

clemens321
clemens321

Reputation: 2098

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.

Upvotes: 2

Related Questions