Not finding an existing file (image)

I am trying to establish that an image exists, to print it, and when not exist to print out that it does not exist. The code I am using is:

[php]$qr_file = $qr_path . $mls . ‘_LC.png’;

if (file_exists($qr_file)) {
print “”;
} else {
print “The file $qr_file, does not exist!!”;
} // endif[/php]

the $qr_file, when printed out via the “does not exist” under the } else { when copied and pasted into the address bar of my browser, displays the image fine. However, the “file_exists” command does not find the file.

I am currently testing this on my LAMP (XAMPP), before putting it online:

The path to the folder containing the image is: http://localhost/_LC/qr_codes/
The $mls is the real estate mls #
The balance of the file name is : _LC.png

When it prints out via the “does not exist” option, the complete file path is:
http://localhost/_LC/qr_codes/LA1234567_LC.png

Again, this url when pasting it into the browser, brings up the image fine. Any thoughts on why the (file_exists($qr_file)) is not detecting that this filename exists? :’(

Try checking the image location without the localhost/_LC part. I believe you need to use either paths like ‘qr_codes/LA1234567_LC.png’ or ‘C:\xampp\htdocs_LC\qr_codes\LA1234567_LC.png’.

Smokey,

That won’t help me online. I want the same variable “name” brought in via an external include file, with the appropriate online or local path. Online, I assume would be regular domain name path and not directory paths?

I use separate external include files for my local and online sites. This way, I can use the same “code” on the page, so I can upload it without having to change the code depending on whether local or online.

I use these separate local and online include files for my database access information, so I just added the appropriate path variable, I need for this, to these local and online include files.

I assume that there must be a way to check for an existing file, when online, which would pertain to a regular domain name, which should also work for my local, localhost path?

The first option (‘qr_codes/image.png’) is a relative path and should work whether locally or on another server. The second example is of course locally and wouldnt help you on a remote server, as the path would most likely be something like ‘/var/www/html…’.

Try the relative path and see if that sorts your issue.

No good either. I use a central domain for the different images that pertain to a number of websites. The reduces the amount of work I need to do when adding, changing or deleting images, only having to do it once instead of on each site.

Consequently, going across domains, I am not sure how I would use a relative path from www… where it would be from www on another domain? They are all on the same server, so possibly using the internal domain id, followed by www…???

Kind of funny that I can use an imported domain name to display an image, but php will not use a domain name to see it is exists? Doesn’t sound right???

file_exists is only for the local file system
however fopen works on local file system as well as urls

[code]

<?php if(!($fh=fopen('http://my.domain.com/image.jpeg','r'))) { die('error'); } else { fclose($fh); echo "Successful'; }[/code]

Laffin,

First, I am using this in a “while” loop, where a different image ID is matched to the ID in the image in each loop.

Now, If I used the actual path in the command, it finds the image as echoing “Success”. However, it also found images “Success” on all of the others in the loop, even though that image ID does not exist?

Second, I need to provide the path as a variable, however when I did, it does NOT find the image. Below is the result of your code, with a variable as the path.

A: File Path: http://localhost/_IP/qr_codes/LA7577880_LC.png

B: Warning: fopen($file_page) [function.fopen]: failed to open stream: No such file or directory in F:_LC\master_files\listing_content.php on line 96

C: Not Found

A: I echoed the path variable, which is the valid path. Entering this path into the address bar of a browser, displays the image (LA7577880_LC.png), fine.

B. This is the warning I get trying to open the path variable. I get this error, whether the image exists, or does not. First, I am not sure why it finds the image, when the path is an actual path, but not as a variable. Second, if the image does NOT exist, I do not want this error to appear, but rather that the image just does not print (display). “No such file or directory in F:_LC\master_files\listing_content.php on line 96”, is the file that I am using to find the image under the path shown above.

C: I changed your “die” comment to print out “Not Found” instead.

Is there a way to check for the image, and then put something into a variable that I can then check with an “if” command? I am really wondering why this is such a problem?

I found the reason, that the open was ok for a static path, but not for the variable. The static path was enclosed in single quotes, which I left in when I changed it to a variable. Removing the single quotes around the variable, and I now get “Success” when the image is found, but still the “Failure Error”, when not found.

My final problem, now is, that instead of the failure error when not found, I don’t want anything to show. How can I eliminate the not found error??

Never Mind… I am all set.

Put back your “die(‘error’);” for failures and changed ‘error’ to a message I want to display if no image.

Thanks for your great help. :smiley:

OOps, my mistake. Although the "die(‘error’) prevents the failure error from displaying it also stops the “while” loop, stopping all of the following items in the loop from displaying.

Is there another method to prevent the error from displaying when the image is not found, but not stopping the “while” loop?? :o

just use an inhouse image, that says not avail

It is the “success” (true) side of the if statement that is giving the error when not finding the image I want to display, not the “false” side, where I would normally have the alternate image.

Thanks anyway, I found a way to suppress the error by inserting the following command just before the if statement: ini_set(‘display_errors’, 0);

I am all set.

Sponsor our Newsletter | Privacy Policy | Terms of Service