Forum Moderators: coopster

Message Too Old, No Replies

Check if remote image exists

Check if remote image exists

         

xray012

8:50 am on Dec 16, 2003 (gmt 0)

10+ Year Member



Hello,

I'me trying to determine if a picture the user wants to add from another weblink, really exists. I cant find the right code to accomplish this;

How can i check "if_exists" a remote image file?

NickCoons

9:12 am on Dec 16, 2003 (gmt 0)

10+ Year Member



Here is an example from the user contributions on the PHP site:

<?php

if (@fclose(@fopen("http://www.example.com", "r"))) {
print("File exists.");
} else {
print("File does not exist.");
}

?>

Also, the notes indicate that with PHP 5.0, file_exists() can be used with some url wrappers, which might make checking for the existance of remote files easier.

xray012

9:27 am on Dec 16, 2003 (gmt 0)

10+ Year Member



thanx alot,It Works Perfect.

seems php is growing more and more powerfull :)

xray012

11:16 am on Dec 16, 2003 (gmt 0)

10+ Year Member



hmmm. i spoke to soon.

The coding is somewhat unstable:

<?php

$image="http://blabla.com/img/image.jpg";

if (@fclose(@fopen("$image", "r"))) {
print("File exists.");
} else {
print("File does not exist.");
}

?>

-sometimes it will display the image
-sometimes it will say the image doesn't exist?

:S

NickCoons

7:12 am on Dec 17, 2003 (gmt 0)

10+ Year Member



I didn't test it.. I just pulled it from the user contributions on the PHP site :-).

When it returns the wrong information, what does it tell you?

There is a single link of code that opens the file, closes it, and then checks to see if those functions returned true (operated correctly). Try removing the @ signs (so that errors are not supressed) and see what errors you receive.

Perhaps it's an issue with the network connection, where sometimes it can open the file and others it can't because the connection to the remote server cannot be made. I use fopen() on remote files all the time and have never really had any issues with it.

chrisef

11:54 am on Jan 15, 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'>");
}

?>