note 41572 deleted from function.file-exists by tomsommer

From: Date: Thu, 15 Apr 2004 06:33:35 +0000
Subject: note 41572 deleted from function.file-exists by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-68151@lists.php.net to get a copy of this message
Note Submitter: mrWoot@mrWoot.com ---- I looked of the code posted previously and fixed somethings. It returns an image (online.gif or offline.gif). It also checks 404 errors and 200 (OK) errors. <?php function FileExist($url){ $head = ''; $url_p = parse_url ($url); if (isset ($url_p['host'])){ $host = $url_p['host']; } else{ return '<img src="offline.gif">'; } if (isset ($url_p["path"])){ $path = $url_p["path"]; } else{ $path = ''; } $fp = fsockopen ($host, 80, $errno, $errstr, 20); if (!$fp){ return '<img src="offline.gif">'; }else{ $parse = parse_url($url); $host = $parse['host']; fputs($fp, "HEAD ".$url." HTTP/1.1\r\n"); fputs($fp, "HOST: ".$host."\r\n"); fputs($fp, "Connection: close\r\n\r\n"); $headers = ""; while (!feof ($fp)){ $headers .= fgets ($fp, 128); } } fclose ($fp); $arr_headers = explode("\n", $headers); $return = '<img src="offline.gif">'; if (isset ($arr_headers[0])){ if(strpos ($arr_headers[0], "404")){ return '<img src="offline.gif">'; } else if(strpos ($arr_headers[0], "200")){ return '<img src="online.gif">'; } } return $return; } // sample code $url = "http://www.goodgle.it/intl/it_it/images/losgo.gif"; $check = FileExist($url); echo $check, ' ', $url; echo '<br />'; $url = "http://www.google.it/intl/it_it/images/logo.gif"; $check = FileExist($url); echo $check, ' ', $url; ?>

« previous php.notes (#68151) next »