PHP – Tester l’existence d’un fichier distant

Vendredi 16 mars, 2012 at 7:00 / 0 comment

1314001210_phpcode_287392

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);
}
Tags: ,

Related Posts

WordPress – Shortcode trouve mon age
WordPress – Shortcode trouve mon age
Développement
NPDS – Méta-mot trouve mon age
NPDS – Méta-mot trouve mon age
Développement
WordPress – Customiser la galerie native de wordpress
WordPress – Customiser la galerie native de wordpress
Développement
ReTex – RssLounge gestionnaire de flux RSS
ReTex – RssLounge gestionnaire de flux RSS
Open-Source, Retour d’expérience

There are no comments yet, add one below.

Leave a Comment


Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*

Vous pouvez utiliser ces balises et attributs HTML : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>




© Hotfirenet.Com - 2002 - 2012