note 72323 added to function.file-exists

From: Date: Sat, 13 Jan 2007 21:10:43 +0000
Subject: note 72323 added to function.file-exists
Groups: php.notes 
Request: Send a blank email to php-notes+get-121608@lists.php.net to get a copy of this message
In regards to the function url_exists below... the reason it's slow is because of fopen(). I tried this function with a loop of 25 urls, and output the time spent using microtime(), which ended up taking 4.6 seconds. I wrote a new function using fsockopen() and tried with the same 25 urls. Time spent, 1.1 seconds. More than 4 times as fast. Another issue with it, I don't know if this is a problem or not, but fopen() returns true if the url results in a 500 Internal Server Error. fsockopen() returns false in this case. I don't know if this is the fastest/most efficient method, but I think it's a nice alternative regardless. Here's the function: <?php function url_exists($url){ if(strstr($url, "http://")) $url = str_replace("http://", "", $url); $fp = @fsockopen($url, 80); if($fp === false) return false; return true; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 74.119.82.139 ---- Manual Page -- http://www.php.net/manual/en/function.file-exists.php Edit -- https://master.php.net/note/edit/72323 Del: integrated -- https://master.php.net/note/delete/72323/integrated Del: useless -- https://master.php.net/note/delete/72323/useless Del: bad code -- https://master.php.net/note/delete/72323/bad+code Del: spam -- https://master.php.net/note/delete/72323/spam Del: non-english -- https://master.php.net/note/delete/72323/non-english Del: in docs -- https://master.php.net/note/delete/72323/in+docs Del: other reasons-- https://master.php.net/note/delete/72323 Reject -- https://master.php.net/note/reject/72323 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#121608) next »