1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <?php
function remote_file_exists ($url)
{
/*
Return error codes:
1 = Invalid URL host
2 = Unable to connect to remote host
*/
$head = "";
$url_p = parse_url ($url);
if (isset ($url_p["host"]))
{ $host = $url_p["host"]; }
else
{ return 1; }
if (isset ($url_p["path"]))
{ $path = $url_p["path"]; }
else
{ $path = ""; }
$fp = fsockopen ($host, 80, $errno, $errstr, 20);
if (!$fp)
{ return 2; }
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;
}
/* utilisation */
$url = "http://google.fr";
if (remote_file_exists ($url))
{ print ($url . " file exists!" ); }
else
{ print ($url . " file doesn't exist!" ); }
?> |
Partager