22, November 2008

Slow Function: Remote File Exists - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Slow Function: Remote File Exists
« previous next »
Pages: [1] Print

Author Topic: Slow Function: Remote File Exists  (Read 618 times)
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« on: Sep 23, 2007, 10:54:36 AM »

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  Angry. 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  Cool

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #1 on: Sep 23, 2007, 11:37:36 AM »

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.


Last blog : Are You Stumbling Yet?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6439
39458 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Sep 23, 2007, 12:28:16 PM »

use cURL


Last blog : Just a better Internet portal provided by Google
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #3 on: Sep 23, 2007, 01:22:01 PM »

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 Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #4 on: Sep 24, 2007, 12:46:58 AM »

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.

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=7148
Tags : php function url exists Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Slow Function: Remote File Exists
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Nov 22, 2008, 08:57:02 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 37.734
Total Topics: 7.649
Total Members: 4.394
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: rdgoetz

35 Guests, 4 Users online :

15 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.