PDA

View Full Version : Image Exists?



JohnKrutsch
Jul 8th, 2002, 09:36 PM
I need to check to see if an image exists on a remote server and if it does, show it. This is what I have tried:


$imgCheck=readfile("http://$domainid:8900/$courseid/images/banner.jpg");
if($imgCheck) echo "<img border=0 src=\"http://$domainid:8900/$courseid/images/banner.jpg\" width=\"550\" height=\"80\" alt=\"banner\">";


So far I can't get it to work out. What I am I missing

Feyd
Jul 8th, 2002, 10:19 PM
$fp = @fopen("http://$domainid:8900/$courseid/images/banner.jpg","r");
if ($fp) {
echo '<img border=0 src="http://'.$domainid.':8900/'.$courseid.'/images/banner.jpg" width="550" height="80" alt="banner">';
} else { echo 'Resource not valid'; }

JohnKrutsch
Jul 8th, 2002, 10:37 PM
Hmmm...When I try that it tells me it is not a valid resourse yet the resource is there. When I take out the @ to check for error messages I get:

Warning: fopen("http://161.28.224.167:8900/BMC_2250IN02/images/banner.jpg","r") - Success in /web/htdocs/uvnet/PHP/WebCT/openfeedback.php on line 45

If I just use:

echo '<img border=0 src="http://'.$domainid.':8900/'.$courseid.'/images/banner.jpg" width="550" height="80" alt="banner">';

where I know there is an image it will show the image, yet if I try to detect it using fopen or readfile it fails. We have several hundred places to check and some of them have the new banner set up and some of them don't, I just want to write one page to deal with it all.

Feyd
Jul 8th, 2002, 10:45 PM
Weird...works for me...wait, you've got password protection on that dir. Are all of these on the same box/ip?

Feyd
Jul 8th, 2002, 11:03 PM
Or you could do



if($img = @GetImageSize('http://'.$domainid.':8900/'.$courseid.'/images/banner.jpg')) {
echo 'image exists';
} else {
echo 'image does not exist';
}


But you are still going to get into the user auth problem (which is why it is telling you it doesn't exist) unless you hardcode the auth into this script (which could be bad juju), or open the file directly on the server file level as opposed to ip/URL. (or you could wrap it in a login)

JohnKrutsch
Jul 8th, 2002, 11:32 PM
Unfortunently they are on diferent servers. When I try this:

if($img = @GetImageSize('http://Username:[email protected]'.$domainid.':8900/'.$courseid.'/images/banner.jpg')) {
echo 'image exists';
} else {
echo 'image does not exist';
}

it still fails. However if I just enter this into the adress bar it works:

http://Username:[email protected]:8900/BMC_2250IN02/images/banner.jpg

firepages
Jul 9th, 2002, 03:41 AM
have a play with the snoopy class ...

http://snoopy.sourceforge.com


this just grabs the image contents, but you should be able to elicit a true or false out of this...



<?
include "Snoopy.class.inc";
$snoopy = new Snoopy;
//$snoopy->user = "joe";
//$snoopy->pass = "bloe";

if($snoopy->fetch("http://www.firepages.com.au/img/v4.jpg"))
{
echo "response code: ".$snoopy->response_code."<br>\n";
echo $snoopy->results;

}
else
echo "error fetching document: ".$snoopy->error."\n";
?>


I am not sure if getimagesize() works on remote files and I assume that you need fopen wrappers turned on if it actually does.

Another interested point, you would need to do something with the code above to chek the content (perhaps the returned headers?) as when you attempt to open a file that does not exists, you still get an OK response, I assume as the web-server dishes out a 404 if the file is not found, I know that snoopy can return just the headers though and this should do the trick??

firepages
Jul 9th, 2002, 03:46 AM
this should work ... you just have to check the $arr array for 'image/jpeg' etc.



<?
include "Snoopy.class.inc";
$snoopy = new Snoopy;

//$snoopy->user = "joe";
//$snoopy->pass = "bloe";

if($snoopy->fetch("http://www.firepages.com.au/img/v4.jpg"))
{
echo "response code: ".$snoopy->response_code."<br>\n";
$arr=$snoopy->headers;
while(list($key,$var)=each($arr)){echo "$key , $var<br>";}
}
else
echo "error fetching document: ".$snoopy->error."\n";
?>