Hi
I’m looking for a way to check if an image exists on a webserver and then if it does exist show it along with a title and some information. If it doesn’t exist I don’t want to show the information at all.
Is it possible to check whether a file exists on another website if I have the exact location of it?
The script I was trying to use is below but it keeps returning that the images are not there even though I know some of them are.
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
I am populating the $filename dynamically from entries in a mysql database.
Any help with this would be appreciated.
Thanks for the replies.
Here’s what I’m using to get the name of the image file:
$filename = “http://www.mainwebsite.co.uk/admin/Stock/mainfolder/“.”$row_stock[Full_Registration]"."1.jpg”;
The site I’m using this code on is not www.mainwebsite.co.uk. Is that why the code will not work?
The image is on the same web server because both websites are hosted on the same sever so do you think its just the path thats wrong?
Absolutely. You need to approach the file path from the point of view of the server, not from an external visitor.
If the file is not on your local server, you will need to use completely different techniques to access the file.
The techniques can vary depending on how much access permission you have to the other server.
I have a feeling that your images are stored in the database relative to the site root, rather than the server root. The path /images/someimage.jpg wouldn’t work on the server because the initial / would specify to start from the server root.
Try prepending the document root to the image path.
If that doesn’t work, paste the full code you’re using along with a filename string that does work, and one that doesn’t (but should).
Where you have the path
$filename = ‘/path/to/foo.txt’;
I suspect that you think of the path in terms of the web browser. That’s not the case though.
PHP is running from the server so the path will be in terms of the server. If your script is located at /home/user1/public_html/index.php then you will need to use that type of path instead.
You can use the server variable called ‘DOCUMENT_ROOT’ to get that path.
As a result, the following sort of thing may be more suitable for you:
$filename = $_SERVER['DOCUMENT_ROOT'] . '/path/to/foo.txt';