note 43846 deleted from function.file-exists by tomsommer

From: Date: Tue, 06 Jul 2004 11:40:08 +0000
Subject: note 43846 deleted from function.file-exists by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72754@lists.php.net to get a copy of this message
Note Submitter: jiquera ---- <? /* based on cvictor's code... a easier to use function, NOTE that the results will be wrong incase: -the target host has a custom 404 it will return its 404 and the script will recognize this as "file found", i added the final url as result on found so you can check if you caught a custom 404 -the host you are running it from has some kinda help script wich returns a custom errorpage "host not found", when you check an invalid url that page is returned to you... so the result is "file found" (took me a day to find that out :S) */ function remote_file_exists ($url , &$errstr) { $errstr = ""; $errors = array($url, "Invalid URL host", "Connection Error", "File does not exist", "Status Code not supported: "); $url_p = parse_url ($url); if(empty($url_p['host']) { $errstr = "1 " . $errors[1]; return false; } $fp = fsockopen ($url_p['host'], 80, $errno, $errstr, 20); if (!$fp) { $errstr = "2 " . $errstr; return false; } else { fputs($fp, 'HEAD ' . $url . " HTTP/1.1\r\n"); fputs($fp, 'HOST: ' . $url_p['host'] . "\r\n"); fputs($fp, "Connection: close\r\n\r\n"); $headers = ""; while (!feof ($fp)) $headers .= fgets ($fp, 128); } fclose ($fp); // for debug //echo nl2br($headers); $arr_headers = explode("\n", $headers); if (isset ($arr_headers[0])) { if(strpos ($arr_headers[0], '200') !== false) { $errstr = "0 " . $errors[0]; return true; } if( (strpos ($arr_headers[0], '404') !== false) || (strpos ($arr_headers[0], '410') !== false)) { $errstr = "3 " . $errors[3]; return false; } if( (strpos ($arr_headers[0], '301') !== false) || (strpos ($arr_headers[0], '302') !== false)) { preg_match("/Location:\s*(.+)\r/i", $headers, $matches); if(!isset($matches[1])) { $errstr = "3 " . $errors[3]; return false; } $nextloc = $matches[1]; return remote_file_exists($nextloc, $errstr); } } preg_match('/HTTP.*(\d\d\d.*)\r/i', $headers, $matches); $errstr = "4 " . $errors[4] . (isset($matches[1])?": $matches[1]":""); return false; } // sample code $urls = Array(); $urls[] = "http://google.com";//ok, folder $urls[] = "http://go3ogle/";//wrong host $urls[] = "http://ntuee.org/camp";//redirect 302 $urls[] = "http://ntuee.org/camp/a/";//redirect to non-existing dest $urls[] = "http://www.google.com/images/hp0.gif";//ok, file $urls[] = "http://www.google.com/images/hp00.gif";//non-existing file $urls[] = "https://download.cc.ntu.edu.tw/ssl/index.php";//auth 403 $urls[] = "https://x-life.com/~pr2/fasfasf";//404 custom foreach($urls as $url) { echo "<b>$url</b><br />"; if(remote_file_exists($url, $errstr)) echo "<u>file exist!</u><br>" . $errstr; else echo "<u>file doesn't exist!</u><br>" . $errstr; print "<br /><br>"; } ; ?>

« previous php.notes (#72754) next »