Vendredi 16 mars, 2012 at 7:00 / 0 comment
Voici 3 fonctions qui finalement permettent de tester l’existence d’un fichier distant.
Nécessite CURL
function getInfos($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_exec($ch);
return curl_getinfo($ch);
}
$infos = getInfos('http://www.hotfirenet.com');
if($infos['http_code'] == 200){
// OK
}else{
// pas ok
}
Apparemment consommatrice car elle télécharge une partie du fichier.
function remote_file_exists ( $url ) {
ini_set('allow_url_fopen', '1');
if (@fclose(@fopen($url, 'r'))) { return true; }
else { return false; }
}
UNIQUEMENT possible depuis PHP5
function sys_file_exists($f = NULL)
{
$h = array();
$ret = FALSE;
if(!is_null($f)):
if(preg_match('/^http|https|ftp/',$f)): //test protocol EXTERN
$h = @get_headers($f);
if(array_key_exists(0,$h)) :
$ret = (bool) preg_match('/200|301/',$h[0]); /* HTTP/1.1 301 DAP (directory) */
endif;
else: //else FS
$ret = (file_exists($f) && is_readable($f));
endif;
endif;
return (($ret === TRUE) ? TRUE : FALSE);
}






There are no comments yet, add one below.