note 88859 modified in function.file-exists by danbrown
| From: | danbrown@php.net | Date: | Wed, 11 Feb 2009 19:36:10 +0000 |
| Subject: | note 88859 modified in function.file-exists by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150509@lists.php.net to get a copy of this message | ||
Hi! There were a solution with @get_headers in one post below, but I thing here is a better one
function, because in this example is a timeout solution included!
<?php
function checkImage($image_url, $timeout = 3) {
$ch = curl_init($image_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, true);
$res = curl_exec($ch);
curl_close($ch);
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/", $res, $matches);
$code = (int) end($matches[1]);
if ($res !== false && $code == 200) {
return ((bool) true);
} else {
return ((bool) false);
}
}
?>
--was--
Hi! There were a solution with @get_headers in one post below, but I thing here is a better one
function, because in this example is a timeout solution included!
<?php
function checkFile($url, $timeout = 3) {
$ch = curl_init ($url) ;
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt ($ch, CURLOPT_HEADER, true );
$res = curl_exec ($ch) ;
curl_close ($ch);
if ($res !== false && preg_match("|200|", $res) === 1) {
return ((bool) true);
}
else {
return ((bool) false);
}
}
?>
http://php.net/manual/en/function.file-exists.php