Forum Moderators: coopster

Message Too Old, No Replies

check remote image

check remote image php

         

chrisef

4:13 pm on Jan 16, 2004 (gmt 0)

10+ Year Member



Hi
I am a complete newbie with php. I've tried to adapt the code below to pick up whether an image file exists, if so display it if not display a "no image exists" image instead.

The image file is picked up by using the article field and adding ".jpg" to it. Is this causing the problem, rather than having a dedicated image column in the database? Or is it just a syntax or bad coding error (Ihave tried using various double and single quotes within code).

If anyone has any ideas I'd be really grateful to hear them.

Thanks

<?php

$image= ("images/"$row_Recordset1['article']".jpg");

if (@fclose(@fopen("$image", "r"))) {
print("<img src='images/' $row_Recordset1['article'] '.jpg' border='1'>");
}

else {
print("<img src='images/noimage.jpg'>");
}

?>

coopster

4:59 pm on Jan 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, chrisef!

You need to concatenate your $image variable together, notice the periods which represent concatenation [php.net]:


$image= 'images/' . $row_Recordset1['article'] . '.jpg';

Also, you really don't seem to be checking for the existence of a remote image here (keyword being remote, as in on another server somewhere). Therefore, I would use PHP's is_file [php.net] function to check for the existence of the file rather than throwing the overhead of a file open and close on it. For example:

$image= 'images/' . $row_Recordset1['article'] . '.jpg';
if (is_file($image)) {
print "<img src=\"$image\" border=\"1\">";
} else {
print '<img src="images/noimage.jpg">';
}

You'll notice that I use single quotes on some of the print statements and double quotes on others. This is because a string literal can be specified in different ways. You can read more about Strings [php.net] for a better understanding.

chrisef

5:23 pm on Jan 19, 2004 (gmt 0)

10+ Year Member



This works perfectly

Thanks for taking the time to explain this Coopster!

I've been tearing my hair out over the last few days!

All the best to you!