Re: Fw: Confirm Remote File exists !
| From: | Stuart Dallas | Date: | Wed, 08 May 2002 18:42:55 +0000 |
| Subject: | Re: Fw: Confirm Remote File exists ! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-96626@lists.php.net to get a copy of this message | ||
On 8 May 2002 at 17:01, John Fishworld wrote:
> I've got a form where people can enter a link into !
>
> What's the best and quickest way of confirming if the link exists !
> Just use fpassthru and see if you get an error ?
> Or has anyone got any better suggestions ??
I wrote the following function a while back. I've only ever used it with http urls so I
don't
know if it works properly for other types. Let me know if it's useful.
function RemoteFileExists($url, $referer = "")
{
$url_parts = parse_url($url);
if ($url_parts["scheme"] != "http" && $url_parts["scheme"]
!= "https" &&
$url_parts["scheme"] != "ftp")
{
return TRUE;
}
$ch = curl_init();
$urltoget =
$url_parts["scheme"]."://".$url_parts["host"].$url_parts["path"];
if (isset($url_parts["query"])) $urltoget .=
"?".$urltoget["query"];
curl_setopt ($ch, CURLOPT_URL, $urltoget);
if ($url_parts["scheme"] == "http" || $url_parts["scheme"] ==
"https")
{
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if (strlen($referer) > 0) curl_setopt($ch, CURLOPT_REFERER, $referer);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$reply = curl_exec($ch);
curl_close($ch);
if ($url_parts["scheme"] == "http" || $url_parts["scheme"] ==
"https")
{
$result = explode(" ", $reply);
$reply = ($result[1] == "200");
}
return $reply;
}
--
Stuart