PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
tstimple
Forum Commoner
Posts: 53 Joined: Wed Jan 21, 2004 10:12 pm
Post
by tstimple » Mon Mar 29, 2004 7:11 am
Does FILE_EXISTS only work on files stored on the local server?
For example,
If I use:
Code: Select all
<?php
$thumbnail="/localdir/filename.jpg";
if(file_exists($thumbnail)){....
}
?>
it returns true.
But if I use:
Code: Select all
<?php
$thumbnail="http://www.somewhere.com/filename.jpg";
if(file_exists($thumbnail)){...
}
?>
it returns false (even though I know the file exists).
Am I doing somthing wrong?
Is there an alternative to checking the existance of a file on a remote host?
thanks,
--TIM
?>
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Mon Mar 29, 2004 7:17 am
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Mon Mar 29, 2004 7:19 am
Code: Select all
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!"); }