I am trying to see if a URL exists or not, and if it does not exist reset the variable containing the URL to an error page/image. However my attempt below sets every instance of PIC_URL to noimage.gif, regardless of if it exists and is accessible or not. What am I missing?
if (@fopen($_REQUEST[$PIC_URL],"r"))
{
$status = "1";
}
else if (!(@fopen($_REQUEST[$PIC_URL],"r"))
{
$status = "2";
}
if ( $status == "2" ) $PIC_URL = "http://www.tricityhomes.com/assets/images/noimage.gif";
use
file_exists
or is_readable, and don’t use raw valueThe test on the else part looks redundant, and did you mean
$_REQUEST['PIC_URL']
?There’s plenty more wrong with it though. To test if a remote file exists, you’d be better off using file_exists, or even rolling your own method to perform a HEAD request.
You might want to try doing something similar to this
Source: http://uk.php.net/manual/en/function.file-exists.php#85246
the reason being is that as has been mentioned checking against the raw value of fopen is not the best idea and also that method does not take account of being redirected to error pages etc.