note 43915 added to function.file-exists
| From: | jiquera at yahoo dot com | Date: | Thu, 08 Jul 2004 11:42:30 +0000 |
| Subject: | note 43915 added to function.file-exists | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72876@lists.php.net to get a copy of this message | ||
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));
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.file-exists.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43915
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43915&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43915&report=yes
Search -- http://master.php.net/manage/user-notes.php