Reputation: 1
I'm working on this simple script which finds an image url based upon database values then echos that to display the image, or if no image is available a default image.
However there is something wrong as it returns the default image every-time. If I replace the default echo line with the $image line, it displays. So from that I know it can find the URL of the image.
<?php
$image = CONST_IMG_URL.$FetchData['capcode'].".jpg";
error_reporting(0);
if (getimagesize($image)) {
echo "<a href='$image'><img src=\"$image\" width='300' height='150'/></a>";
}
else {
echo "<img src='".plugins_url()."/plugins/images/image_not_found.jpg' width='300' height='150'/>";}
?>
Upvotes: 0
Views: 896
Reputation: 4173
In your example, getimagesize
isn't doing anything productive.. I would suggest using file_exists
..
In order to have file_exists
work correctly - you need to give the full path (not a URI).
So you could do something like: $_SERVER['DOCUMENT_ROOT'] . '/path/to/images/' . $image;
like so:
if(file_exists( '/full/path/to/image/dir/' . $image ))
{
echo "<a href='$image'><img src=\"$image\" width='300' height='150'/></a>";
} else {
echo "<img src='".plugins_url()."/plugins/images/image_not_found.jpg' width='300' height='150'/>";
}
Explanation
getimagesize
- This returns an array
of attributes relating to the image, rather than explicitly returning if the image exists or not, in fact, if the image does not exist it will throw a warning rather than returning false
as you'd expect.
file_exists
- this checks for the file, and returns true
if it exists, and false
if the file does not exist - this is exactly what you're looking for in your scenario
Upvotes: 1
Reputation: 7597
You should use file_exists function instead of getimagesize:
<?php
$image = CONST_IMG_URL.$FetchData['capcode'].".jpg";
error_reporting(0);
if (file_exists($image)) {
echo "<a href='$image'><img src=\"$image\" width='300' height='150'/></a>";
}
else {
echo "<img src='".plugins_url()."/plugins/images/image_not_found.jpg' width='300' height='150'/>";}
?>
Upvotes: 0
Reputation: 38456
As a guess, your CONST_IMG_URL
is a path that's relative to the website - not the file-system.
The getimagesize()
method finds images on the file-system itself using relative or absolute path names (i.e. /var/www/images/uploaded/pic.jpg
or images/uploaded/pic.jpg
). It can also find images via URLs such as http://example.com/images/uploaded/pic.jpg
.
If CONST_IMG_URL
is a path, without the domain portion, getimagesize()
will look at the file-system for that image and there's a good chance that it doesn't exist. For instance, if CONST_IMG_URL
is /images/uploaded/
, the method will check on the server for that absolute path - which won't exist as it treats the leading /
as "the root of the server", not "inside your website's folder".
To remedy this, you can take two approaches (if you want to keep using getimagesize()
).
The first is the make CONST_IMG_URL
an absolute path that includes your domain:
define('CONST_IMG_URL', 'http://domain.com/images/uploaded');
The second would be to append the full document-root to the path when you're calling getimagesize():
if (getimagesize($_SERVER['DOCUMENT_ROOT'] . '/' . $image)) {
If you're going to go with #2 above, I would actually recommend using a different method, file_exists()
instead. This function will simply check if the file exists opposed to loading the file to pull parameter information out of it (width, height, etc) and will make page-loads slightly more efficient =].
Upvotes: 0
Reputation: 1730
You should use next code:
try {
$img = @getimagesize($image);
echo "<a href='$image'><img src=\"$image\" width='300' height='150'/></a>";
} catch(Exception $e) {
echo "<img src='".plugins_url()."/plugins/images/image_not_found.jpg' width='300' height='150'/>";
}
Upvotes: 0