Having a file_exists issue

(Sorry if you’ve seen this is pieces. This site is hard to use on a phone.)
I’m working on a section of my site for herbs with dynamically created pages and I’m trying to do an if else tag to show one picture if it exists and another picture if it doesn’t.

This is my current code for the image of else

$imgsrc = "/new/herbs/pages/imgs/$herb.jpg"; echo "<img src='$imgsrc' alt='$herb'>"; if (file_exists("$imgsrc")) echo "<img src='$imgsrc' alt='$herb'>"; else echo "<img src='new/herbs/pages/imgs/imgherb.jpg' alt='$herb'>";

For some reason, it doesn’t recognize that the file exists and still shows the image in the else tag.
I have made this into a function, I’ve added curly brackets, I’ve used the manual link to the folder of images, and I’ve used $_SERVER[‘DOCUMENT_ROOT’]. I still have the same issue.

If I link to the image manually, it works fine; with and without the variable $imgsrc. So I am at a total loss as to what it could be.
Any ideas?

The first thing I would do is to organize your folders a little bit better.

For example I do this with my own website


That way I could simply do the following

$filename = 'assets/large/img-photos-1598866463.jpg';

if (file_exists($filename)) {
    echo '<img src="' . $filename . '" alt="MyImage">';
} else {
    echo 'The file ' . $filename . ' does not exist';

Personally, I don’t use file_exists() as the path and filename is stored in my database table and if it’s not there I know my code isn’t working properly. Though you could be doing something else that requires it.

Yeah, I know my folders look a little funky, but right now it’s in development (the ‘new’ directory), and the herbs have their own section on the site. My pages are all done dynamically, so it actually comes out to “index.php?herb=acacia”

The main site is actually built just like you mentioned with css, assets, images, etc. Since the herbs have their own section, I decided to put the images with them so they’re all in one spot, which I guess I could do in the main /images/ folder, too.

Anyway, as to your reply. That’s really just a different way of typing the code I already have. I have tried it, though, and it’s still not working properly. Everytime I search for answers, they’re all just about using the localhost directory instead of a URL. Hence why I used $_SERVER[‘DOCUMENT_ROOT’].
I have tried urls, I’ve tried different folders, I’ve tried everything, but it’s like it doesn’t recognize the fact that the image exists and I just don’t understand why.

Just realized I posted the one without the host root. Here’s what my code looks like now:

$imgsrc = $_SERVER['DOCUMENT_ROOT'] . '/new/herbs/pages/imgs/$herb.jpg';
		 function showHerb($imgsrc){
	   if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/new/herbs/pages/imgs/$herb.jpg')) 
	     echo "<img src='$imgsrc' alt='$herb'>";
       echo "<img src='/new/herbs/pages/imgs/imgherb.jpg' alt='$herb'>";

I tried your method, in case I was wrong. Strange thing is, it keeps telling me the image doesn’t exist. I even copied/pasted the url… the image showed up… so it obviously exists and the path is correct. I don’t understand why it’s not finding this stupid picture.

File_exists() goes through the filesystem to reference the file (yes you can make it work with a fully qualified URL, but that will result in it taking about a 1000 times longer to execute.) The value you give it is NOT a URL. A leading slash / in it refers to the root of the current hard-disk.

On the other hand, because it is fetched by the browser, the value you put into the <img src='...' attribute is a URL. You are actually supplying a relative URL, which the browser combines with the URL of the page it is being output on to arrive at the actual URL that gets requested. A leading slash / in a relative URL makes it a domain relative URL. The browser replaces any part of the current page’s URL after the domain name with whatever the domain relative URL is.

You also switched from the first post of putting double-quotes around the $imgsrc = “…” value containing $herb, to using single-quotes around that value. This caused the $herb variable to go from being replaced with its contents to just being the characters - $, h, e, r, and b.

  1. You must use double-quotes around a php string containing a php variable.
  2. For file_exists() to work, you must build a correct filesystem path to where the file is, and for the value you put into the <img src='...' attribute, you must build a correct URL to where the file gets fetched from.

Yeah, I realized that. My bad. I have it back with double quotes, still not working. I have tried multiple ways of calling to the path of the image, and it just doesn’t seem to want to work.

The following logic should work -


// input - $herb, which may or may not have an image at /new/herbs/pages/imgs/$herb.jpg
// processing - test if image file exists, else use default imgherb
// output - <img > tag

// path to images
$path = '/new/herbs/pages/imgs/';

// if specific image exists, use $herb as the filename, else use imgherb
$filename = file_exists($_SERVER['DOCUMENT_ROOT'] . "$path$herb.jpg") ? $herb : 'imgherb';

$imgsrc = "$path$filename.jpg";

echo "<img src='$imgsrc' alt='$herb'>";

If it doesn’t, it means that there’s something going on the we cannot see from where we are at. Either the actual value in $herb, the path, the filename, the extension, or the capitalization of any of these (on a case-sensitive file system) is not what is being assumed and you would need to provide more specific information about these values.

1 Like

That did it! Thanks much! And it looks so much better than what I originally had and all the other things I was trying.

Sponsor our Newsletter | Privacy Policy | Terms of Service