Meth0d
Sun 23 September 2007, 09:54 am GMT +0200
i have a problem that i am sure a lot of you have run into before... does a remote file exist? a function that works just like PHP's built in "file_exists", but for remote files, or URL's, whatever you want to call it. searching the PHP manual, this is the function I have found and currently am using:
//check if an external file exists
function url_exists($url) {
}
?>
the problem is that it is very slow.. just doing a simple parse time test with the function disabled and enabled, here are the results. the number of elements that access the function for this example is about 15, so its looped that many times.
using the function: Parsed in 2.796 secs
not using the function (to check if the elements exist before showing them): Parsed in 0.228 secs
so you can clearly see the 2.5 second difference there, which is pretty bad >:(. if your curious as to WHY i need this function or why its looped so many times, its for user avatars that arent uploaded through the script, when they provide a URL to that avatar in their profile. so for example on a forum thread with 15 entries, all with an avatar, this function is looped that many times and would cause the page to load this slow.
my question is, is there are better way to check if a file exists on an external server? preferably much faster than what I am doing here, because this just won't work 8)
//check if an external file exists
function url_exists($url) {
$url = str_replace(" ", "%20", $url);
$a_url = parse_url($url);
if (!isset($a_url['port'])) $a_url['port'] = 80;
$errno = 0;
$errstr = ''; $timeout = 15;
if(isset($a_url['host']) && $a_url['host']!=gethostbyname($a_url['host'])){
$fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr, $timeout);
if (!$fid) return false;
$page = isset($a_url['path']) ?$a_url['path']:'';
$page .= isset($a_url['query'])?'?'.$a_url['query']:'';
fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host: '.$a_url['host']."\r\n\r\n");
$head = fread($fid, 4096); fclose($fid);
return preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head);
} else {
return false;
}
}
?>
the problem is that it is very slow.. just doing a simple parse time test with the function disabled and enabled, here are the results. the number of elements that access the function for this example is about 15, so its looped that many times.
using the function: Parsed in 2.796 secs
not using the function (to check if the elements exist before showing them): Parsed in 0.228 secs
so you can clearly see the 2.5 second difference there, which is pretty bad >:(. if your curious as to WHY i need this function or why its looped so many times, its for user avatars that arent uploaded through the script, when they provide a URL to that avatar in their profile. so for example on a forum thread with 15 entries, all with an avatar, this function is looped that many times and would cause the page to load this slow.
my question is, is there are better way to check if a file exists on an external server? preferably much faster than what I am doing here, because this just won't work 8)