Sublime directory Surf the web anonymous Pagerank Monitor


Slow Function: Remote File Exists

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) {
	
$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($fid4096); 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)

Mind_nl
Sun 23 September 2007, 10:37 am GMT +0200
I think the 2.5 seconds isn't too bad, considering you are connecting to a remote site 15 times. This comes down to connecting to a remote site to check if the file exists in 0.17 seconds.
If the time it takes is really a problem, why bother checking at all? Just include the remote url in your img tag.

olaf
Sun 23 September 2007, 11:28 am GMT +0200
use cURL

Nikolas
Sun 23 September 2007, 12:22 pm GMT +0200
Just remove the gethostbyname() call from your function. cURL is great but it uses fsocket too, so I guess the fastest way is a modified version of what you do now :)

Meth0d
Sun 23 September 2007, 11:46 pm GMT +0200
thanks for the help guys, i decided the best solution was to take nikolas' suggestion, and move the check to see if the avatar exists to actually when updating the profile (dont know why i didnt do this before..).. and simply removing the check from the function that checks if it exists, since its done when updating.

Archive for SMF v1.00 by N.P. Valid XHTML 1.0 Transitional