Re: Why not 'file_exists()' for URLs?
| From: | Mike | Date: | Tue, 20 Mar 2001 10:17:50 +0000 |
| Subject: | Re: Why not 'file_exists()' for URLs? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-44710@lists.php.net to get a copy of this message | ||
On 19 Mar 2001 06:53:00 -0800, david@davidgunter.com ("David G.") wrote:
> Since fopen/fread/fclose all work with standard URLs, why not allow
> file_exists() to work with URLs as well?
> What is the procedure to make such a request?
Try this:
function url_exist($url) {
$url_parts = parse_url($url);
$fp = fsockopen($url_parts["host"], 80);
if (!$fp) return false;
$path = $url_parts["path")?$url_parts["path"]:"/";
fwrite($fp, "GET $path HTTP/1.0\r\n\r\n");
$response = fgets($fp, 500);
preg_match("/^HTTP\/\d\.\d[ \t]+(\d+)[ \t]*([^\r]*)/", $response, $matches);
switch ($matches[1]) {
case 404: // HTTP error not found
return false;
case 200: // HTTP OK
return true;
default:
return false; // other responses probably counts false
}
}