Voici 3 fonctions qui finalement permettent de tester l’existence d’un fichier distant.
Nécessite CURL
[fusion_builder_container hundred_percent= »yes » overflow= »visible »][fusion_builder_row][fusion_builder_column type= »1_1″ background_position= »left top » background_color= » » border_size= » » border_color= » » border_style= »solid » spacing= »yes » background_image= » » background_repeat= »no-repeat » padding= » » margin_top= »0px » margin_bottom= »0px » class= » » id= » » animation_type= » » animation_speed= »0.3″ animation_direction= »left » hide_on_mobile= »no » center_content= »no » min_height= »none »][php]
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(‘https://hotfirenet.com/blog’);
if($infos[‘http_code’] == 200){
// OK
}else{
// pas ok
}
[/php]
Apparemment consommatrice car elle télécharge une partie du fichier.
[/fusion_builder_column][fusion_builder_column type= »1_1″ background_position= »left top » background_color= » » border_size= » » border_color= » » border_style= »solid » spacing= »yes » background_image= » » background_repeat= »no-repeat » padding= » » margin_top= »0px » margin_bottom= »0px » class= » » id= » » animation_type= » » animation_speed= »0.3″ animation_direction= »left » hide_on_mobile= »no » center_content= »no » min_height= »none »][php]
function remote_file_exists ( $url ) {
ini_set(‘allow_url_fopen’, ‘1’);
if (@fclose(@fopen($url, ‘r’))) { return true; }
else { return false; }
}
[/php]
UNIQUEMENT possible depuis PHP5
[/fusion_builder_column][fusion_builder_column type= »1_1″ background_position= »left top » background_color= » » border_size= » » border_color= » » border_style= »solid » spacing= »yes » background_image= » » background_repeat= »no-repeat » padding= » » margin_top= »0px » margin_bottom= »0px » class= » » id= » » animation_type= » » animation_speed= »0.3″ animation_direction= »left » hide_on_mobile= »no » center_content= »no » min_height= »none »][php]
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);
}
[/php][/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]
Merci pour cet article très complet. J’ai essayé les fonctions remote_file_exists et sys_file_exists, mais le resultat reste le même: elles détectent bien l’absence ou la présence d’un fichier, mais uniquement un fichier hébergé sur le même serveur. Pour le fichier http://www.cyclauto.fr/temp/banniere_cyclauto_468x60_01.jpg situé sur un serveur externe, il est toujours détecté ok alors qu’il n’existe pas. Pourtant je suis en php 5.2.17. Je suppose qu’il doit y avoir un paramètrage serveur qui rend la fonction inutilisable.
Il est possible que cela vienne de la config de ton serveur, tu es sur un mutu ou sur un dédié ?
Merci pour cet article très complet. J’ai essayé les fonctions remote_file_exists et sys_file_exists, mais le resultat reste le même: elles détectent bien l’absence ou la présence d’un fichier, mais uniquement un fichier hébergé sur le même serveur. Pour le fichier http://www.cyclauto.fr/temp/banniere_cyclauto_468x60_01.jpg situé sur un serveur externe, il est toujours détecté ok alors qu’il n’existe pas. Pourtant je suis en php 5.2.17. Je suppose qu’il doit y avoir un paramètrage serveur qui rend la fonction inutilisable.
Il est possible que cela vienne de la config de ton serveur, tu es sur un mutu ou sur un dédié ?