3

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,

Artūrs Eimanis
  • 578
  • 3
  • 5
  • 22
  • 1
    Any chance of modifying the file when uploading them? So that the possible PHP script responsible for uploading converts everything to lower case. Or are files handled manually by someone else? – Per Enström May 10 '16 at 11:17
  • By files you mean the images? Well they emailed the images to me. And I just put them in the folder and upload them to the server –  May 10 '16 at 11:18
  • 1
    @Theekopje You can just rename them yourself then right? – Lost F. May 10 '16 at 11:20
  • just rename the files to be lowercase or uppercase on linux/unix lowercase and uppercase letter matter , while on windows it is the same. – Sergey May 10 '16 at 11:21
  • 1
    Why are you trying to check, if the file exists with the full URL? – gre_gor May 10 '16 at 11:21
  • No i can't.. Well I can rename them but not the type of file from JPG to jpg. atleast I don't know how to. Tried looking it up but couldn't find a solution. –  May 10 '16 at 11:21
  • 1
    Find a way to rename the images before uploading them, preferably in a script. Input sanitization is your friend. – Joost May 10 '16 at 11:21
  • @gre_gor [this post](http://stackoverflow.com/questions/7991425/php-how-to-check-if-image-file-exists) –  May 10 '16 at 11:22
  • you can for sure rename them on linux host , but it is quite hard to do on windows. on window rename it to something totally different like aaa.bbb and then back to filename.jpg – Sergey May 10 '16 at 11:24
  • any idea how to do this? @Joost –  May 10 '16 at 11:24
  • @Theekopje What OS are you running on your dev/uploading machine (not the server)? If you use Windows you need to do two steps to change the case of a file. First change `foo.JPG` into `foo.tmp.jpg` and then back to `foo.jpg`. – h2ooooooo May 10 '16 at 11:25
  • if I do that @Sergey it just says for example: DOC_202.jpg but when i check in the file properties the file is still JPG –  May 10 '16 at 11:26
  • Windows @h2ooooooo –  May 10 '16 at 11:26
  • 1
    @Theekopje As I just edited, make sure you rename your file first and THEN change the extension. Windows won't let you change the case of a file as it expects you didn't change anything so you need to force it to change it by changing something that isn't just case. – h2ooooooo May 10 '16 at 11:27
  • 1
    @h2ooooooo Ah, the wonderful quirks of Windows. – Per Enström May 10 '16 at 11:44

2 Answers2

2

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.

clemens321
  • 2,103
  • 12
  • 18
  • This worked for me but I cant figure out why because [this post](http://stackoverflow.com/questions/7991425/php-how-to-check-if-image-file-exists) suggested to use full link? But thanks! –  May 10 '16 at 11:28
  • That's weird. You should use the physical local address of the file. – Per Enström May 10 '16 at 11:31
  • 2
    @PerEnström It is the relative physical local file name, not the relative url. See my edit, linux (the server) uses forward slashes as directory separator – clemens321 May 10 '16 at 11:35
  • @clemens321 Yes, I was referring to the linked question by Theekopje. Not your answer! :) – Per Enström May 10 '16 at 11:37
  • 1
    About using URL instead of physical address, check this comment on PHP.net: http://php.net/manual/en/function.file-exists.php#75064 – Per Enström May 10 '16 at 11:37
0

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

Per Enström
  • 902
  • 8
  • 33