note 43915 deleted from function.file-exists by tomsommer
| From: | tomsommer@php.net | Date: | Thu, 08 Jul 2004 13:09:48 +0000 |
| Subject: | note 43915 deleted from function.file-exists by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72880@lists.php.net to get a copy of this message | ||
Note Submitter: jiquera@yahoo.com
----
here's an easy to use version of your coed cvictor note that its very slow when it has to
handle alot of redirects.
and when sites have their own curtom 404 page its returns a file exists because the 404 is
returned... if a file exists the place of the returned file is stored in $errstr so you can check
whether you caught the 404 page or not.
<?php
function remote_file_exists ($url , &$errstr)
{
$errstr = "";
$errors = array($url, "Invalid URL host", "Connection Error", "File does
not exist", "Status Code not supported");
$url_p = parse_url ($url);
if(empty($url_p['host']))
{ $errstr = "1 " . $errors[1];
return false;
}
$fp = fsockopen ($url_p['host'], 80, $errno, $errstr, 10);
if (!$fp)
{ $errstr = "2 " . $errstr;
return false;
}
else
{
fputs($fp, 'HEAD ' . $url . " HTTP/1.1\r\n");
fputs($fp, 'HOST: ' . $url_p['host'] . "\r\n");
fputs($fp, "Connection: close\r\n\r\n");
$headers = "";
while (!feof ($fp))
$headers .= fgets ($fp, 128);
}
fclose ($fp);
// for debug
//echo nl2br($headers);
$arr_headers = explode("\n", $headers);
if (isset ($arr_headers[0]))
{
if(strpos ($arr_headers[0], '200') !== false)
{ $errstr = "0 " . $errors[0];
return true;
}
if( (strpos ($arr_headers[0], '404') !== false) || (strpos ($arr_headers[0],
'410') !== false))
{ $errstr = "3 " . $errors[3];
return false;
}
if( (strpos ($arr_headers[0], '301') !== false) || (strpos ($arr_headers[0],
'302') !== false))
{
preg_match("/Location:\s*(.+)\r/i", $headers, $matches);
if(!isset($matches[1]))
{ $errstr = "3 " . $errors[3];
return false;
}
$nextloc = $matches[1];
return remote_file_exists($nextloc, $errstr);
}
}
preg_match('/HTTP.*(\d\d\d.*)\r/i', $headers, $matches);
$errstr = "4 " . $errors[4] . (isset($matches[1])? ": " . $matches[1] :
"");
return false;
}
?>
a way simpler and faster solution (though a bit hairy):
<?php
function remote_file_exists2 ($url)
{
return @fclose(fopen($url,r));
}
?>