<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/features.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'features.remote-files.php',
    1 => 'Utilisation des fichiers &agrave; distance',
    2 => 'Utilisation des fichiers &agrave; distance',
  ),
  'up' => 
  array (
    0 => 'features.php',
    1 => 'Caract&eacute;ristiques',
  ),
  'prev' => 
  array (
    0 => 'features.file-upload.errors.seealso.php',
    1 => 'Voir aussi',
  ),
  'next' => 
  array (
    0 => 'features.connection-handling.php',
    1 => 'Gestion des connexions',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'features/remote-files.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="features.remote-files" class="chapter">
 <h1 class="title">Utilisation des fichiers à distance</h1>

 <p class="para">
  Aussi longtemps que le support des gestionnaires d&#039;URL (&quot;URL fopen wrapper&quot;)
  est activé dans le <var class="filename">php.ini</var>, avec l&#039;option <strong class="option unknown">allow_url_fopen</strong>,
  il est possible d&#039;utiliser des URL (<abbr title="Hypertext Transfer Protocol">HTTP</abbr> et <abbr title="File Transfer Protocol">FTP</abbr>)
  avec la majorité des fonctions qui utilisent un
  nom de fichier comme paramètre. Cela inclut notamment
  <span class="function"><a href="function.include.php" class="function">include</a></span>,
  <span class="function"><a href="function.include-once.php" class="function">include_once</a></span>,
  <span class="function"><a href="function.require.php" class="function">require</a></span> et
  <span class="function"><a href="function.require-once.php" class="function">require_once</a></span>
  (<strong class="option unknown">allow_url_include</strong> doit être actif pour les utiliser).
  Se reporter à <a href="wrappers.php" class="xref">Liste des protocoles et des gestionnaires support&eacute;s</a> pour plus d&#039;informations
  sur les protocoles supportés par PHP.
 </p>
 <p class="para">
  Par exemple, il est possible de suivre l&#039;exemple suivant pour ouvrir un
  fichier sur un serveur web distant, analyser les résultats
  pour extraire les informations nécessaires, et ensuite
  les utiliser dans une requête de base de données, ou
  simplement éditer les informations dans le style du site.
 </p>
 <p class="para">
  <div class="example" id="example-1">
   <p><strong>Exemple #1 Connaître le titre d&#039;une page distante</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$file </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen </span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$file</span><span style="color: #007700">) {<br />  echo </span><span style="color: #DD0000">"&lt;p&gt;Impossible de lire la page.\n"</span><span style="color: #007700">;<br />  exit;<br />}<br />while (!</span><span style="color: #0000BB">feof </span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">)) {<br />    </span><span style="color: #0000BB">$line </span><span style="color: #007700">= </span><span style="color: #0000BB">fgets </span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">, </span><span style="color: #0000BB">1024</span><span style="color: #007700">);<br />    </span><span style="color: #FF8000">/* Cela ne fonctionne que si le titre et ses balises sont sur une seule ligne */<br />    </span><span style="color: #007700">if (</span><span style="color: #0000BB">preg_match </span><span style="color: #007700">(</span><span style="color: #DD0000">"@\&lt;title\&gt;(.*)\&lt;/title\&gt;@i"</span><span style="color: #007700">, </span><span style="color: #0000BB">$line</span><span style="color: #007700">, </span><span style="color: #0000BB">$out</span><span style="color: #007700">)) {<br />        </span><span style="color: #0000BB">$title </span><span style="color: #007700">= </span><span style="color: #0000BB">$out</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br />        break;<br />    }<br />}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  Il est aussi possible d&#039;écrire des fichiers sur un serveur FTP
  (à condition d&#039;être connecté en tant qu&#039;utilisateur ayant les bons
  droits d&#039;accès). Seuls de nouveaux fichiers peuvent être créés avec
  cette méthode ; toute tentative d&#039;écraser un fichier existant fera que
  l&#039;appel à <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> échouera.
 </p>
 <p class="para">
  Pour se connecter avec un utilisateur autre qu&#039;anonyme, il faut
  spécifier un nom d&#039;utilisateur (et certainement le mot de passe) dans
  l&#039;URL, comme <code class="literal">ftp://user:password@ftp.example.com/path/to/file</code>.
  (Il est possible d&#039;utiliser le même type de syntaxe pour accéder
  aux fichiers via <abbr title="Hypertext Transfer Protocol">HTTP</abbr> lorsqu&#039;ils nécessitent une
  identification simple).
 </p>
 <p class="para">
  <div class="example" id="example-2">
   <p><strong>Exemple #2 Stocker des données sur un serveur distant</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$file </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen </span><span style="color: #007700">(</span><span style="color: #DD0000">"ftp://ftp.example.com/incoming/outputfile"</span><span style="color: #007700">, </span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$file</span><span style="color: #007700">) {<br />  echo </span><span style="color: #DD0000">"&lt;p&gt;Impossible d'ouvrir le fichier distant pour écriture.\n"</span><span style="color: #007700">;<br />  exit;<br />}<br /></span><span style="color: #FF8000">/* Écriture des données. */<br /></span><span style="color: #0000BB">fwrite </span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">, </span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'HTTP_USER_AGENT'</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose </span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    On pourrait avoir l&#039;idée, à partir de
    l&#039;exemple ci-dessus, d&#039;utiliser la même technique pour
    écrire dans un fichier de log distant. Malheureusement
    cela ne fonctionnerait pas car l&#039;appel à <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>
    échouera si le fichier distant existe déjà. Pour faire de la
    journalisation distribuée, il est recommandé de plutôt regarder du côté de
    <span class="function"><a href="function.syslog.php" class="function">syslog()</a></span>.
   </p>
  </p></blockquote>
 </p>
</div>
<?php manual_footer($setup); ?>