note 40038 modified in function.file-exists by nlopess
| From: | nlopess@php.net | Date: | Sat, 13 Mar 2004 21:28:39 +0000 |
| Subject: | note 40038 modified in function.file-exists by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-66778@lists.php.net to get a copy of this message | ||
A usefull code to check file existance on a remote web server.
function remote_file_exists ($url)
{
$head = "";
$url_p = parse_url ($url);
if (isset ($url_p["host"]))
{ $host = $url_p["host"]; }
else
{ return false; }
if (isset ($url_p["path"]))
{ $path = $url_p["path"]; }
else
{ $path = ""; }
$fp = fsockopen ($host, 80, $errno, $errstr, 20);
if (!$fp)
{ return false; }
else
{
$parse = parse_url($url);
$host = $parse['host'];
fputs($fp, "HEAD ".$url." HTTP/1.1\r\n");
fputs($fp, "HOST: ".$host."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
$headers = "";
while (!feof ($fp))
{ $headers .= fgets ($fp, 128); }
}
fclose ($fp);
$arr_headers = explode("\n", $headers);
$return = false;
if (isset ($arr_headers[0]))
{ $return = strpos($arr_headers[0], "404") !== false; }
return $return;
}
// sample code
$url = "http://www.google.it/intl/it_it/images/logo.gif";
if (remote_file_exists ($url))
{ print ($url . " file exists!"); }
else
{ print ($url . " file doesn't exist!"); }
--was--
A usefull code to check file existance on a remote web server.
function remote_file_exists ($url)
{
$head = "";
$url_p = parse_url ($url);
if (isset ($url_p["host"]))
{ $host = $url_p["host"]; }
else
{ return false; }
if (isset ($url_p["path"]))
{ $path = $url_p["path"]; }
else
{ $path = ""; }
$fp = fsockopen ($host, 80, $errno, $errstr, 20);
if (!$fp)
{ return false; }
else
{
fputs($fp, "HEAD ".$url." HTTP/1.1\r\n");
fputs($fp, "HOST: dummy\r\n");
fputs($fp, "Connection: close\r\n\r\n");
$headers = "";
while (!feof ($fp))
{ $headers .= fgets ($fp, 128); }
}
fclose ($fp);
$arr_headers = explode("\n", $headers);
$return = false;
if (isset ($arr_headers[0]))
{ $return = strpos($arr_headers[0], "404") !== false; }
return $return;
}
// sample code
$url = "http://www.google.it/intl/it_it/images/logo.gif";
if (remote_file_exists ($url))
{ print ($url . " file doesn't exists!"); }
else
{ print ($url . " file exists!"); }
http://www.php.net/manual/en/function.file-exists.php