<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.filesystem.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.file-get-contents.php',
    1 => 'file_get_contents',
    2 => 'Lit tout un fichier dans une cha&icirc;ne',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Fonctions sur les syst&egrave;mes de fichiers',
  ),
  'prev' => 
  array (
    0 => 'function.file-exists.php',
    1 => 'file_exists',
  ),
  'next' => 
  array (
    0 => 'function.file-put-contents.php',
    1 => 'file_put_contents',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/filesystem/functions/file-get-contents.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.file-get-contents" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">file_get_contents</h1> 
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">file_get_contents</span> &mdash; <span class="dc-title">Lit tout un fichier dans une chaîne</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.file-get-contents-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>file_get_contents</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$filename</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$use_include_path</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span></span> <code class="parameter">$context</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$offset</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></span> <code class="parameter">$length</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span><br>): <span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   Identique à la fonction <span class="function"><a href="function.file.php" class="function">file()</a></span>, hormis le fait que
   <span class="function"><strong>file_get_contents()</strong></span> retourne le fichier
   <code class="parameter">filename</code> dans une chaîne, à partir
   de la position <code class="parameter">offset</code>, et jusqu&#039;à
   <code class="parameter">length</code> octets.
   En cas d&#039;erreur, <span class="function"><strong>file_get_contents()</strong></span> retourne
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
  </p>
  <p class="para">
   <span class="function"><strong>file_get_contents()</strong></span> est la façon recommandée pour lire
   le contenu d&#039;un fichier dans une <a href="language.types.string.php" class="link">chaîne de caractères</a>. Elle utilise un buffer en
   mémoire si ce mécanisme est supporté par le système, afin d&#039;améliorer
   les performances.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Si l&#039;on ouvre une URI avec des caractères spéciaux, comme des espaces, il faut
    encoder cette URI avec la fonction <span class="function"><a href="function.urlencode.php" class="function">urlencode()</a></span>.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.file-get-contents-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">filename</code></dt>
     <dd>
      <p class="para">
       Nom du fichier à lire.
      </p>
     </dd>
    
    
     <dt><code class="parameter">use_include_path</code></dt>
     <dd>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        La constante <strong><code><a href="filesystem.constants.php#constant.file-use-include-path">FILE_USE_INCLUDE_PATH</a></code></strong>
        peut être utilisée pour déclencher la recherche dans le
        <a href="ini.core.php#ini.include-path" class="link">chemin d&#039;inclusion</a>.
        Ceci est impossible si <a href="language.types.declarations.php#language.types.declarations.strict" class="link">strict typing</a>
        est activé, car <strong><code><a href="filesystem.constants.php#constant.file-use-include-path">FILE_USE_INCLUDE_PATH</a></code></strong> est un
        <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>. Il convient d&#039;utiliser <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> à la place.
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
      <p class="para">
       Une ressource de contexte valide, créée avec la fonction
       <span class="function"><a href="function.stream-context-create.php" class="function">stream_context_create()</a></span>. S&#039;il n&#039;y a
       pas besoin d&#039;utiliser un contexte particulier, il est possible
       d&#039;ignorer ce paramètre en affectant la valeur <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">offset</code></dt>
     <dd>
      <p class="para">
       La position à partir de laquelle on commence à lire dans le flux original.
       Une position négative compte à partir de la fin du flux.
      </p>
      <p class="para">
       Le déplacement dans le fichier (<code class="parameter">offset</code>) n&#039;est
       pas supporté sur des fichiers distants. Toute tentative de se déplacer
       dans un fichier qui n&#039;est pas local, cela peut fonctionner sur
       les petits déplacements, mais le comportement peut ne pas être attendu
       car le processus utilise le flux du buffer.
      </p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       La taille maximale des données à lire. Le comportement par défaut est de
       lire jusqu&#039;à la fin du fichier. Ce paramètre s&#039;applique sur le flux traité
       par les filtres.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.file-get-contents-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Retourne les données lues  ou <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> si une erreur survient.
  </p>
  <div class="warning"><strong class="warning">Avertissement</strong><p class="simpara">
 Cette fonction peut retourner <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, mais elle peut aussi retourner une valeur équivalent à <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
 Veuillez lire la section sur les <a href="language.types.boolean.php" class="link">booléens</a> pour plus d&#039;informations.
 Utilisez l&#039;<a href="language.operators.comparison.php" class="link">opérateur ===</a>
 pour tester la valeur de retour exacte de cette fonction.
</p></div>
 </div>


 <div class="refsect1 errors" id="refsect1-function.file-get-contents-errors">
  <h3 class="title">Erreurs / Exceptions</h3>
  <p class="para">
   Émet une alerte de type <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> si,
   <code class="parameter">filename</code> ne peut être trouvé, si le
   paramètre <code class="parameter">length</code> est plus petit que zéro,
   ou si le déplacement à la position <code class="parameter">offset</code>
   spécifié dans le flux échoue.
  </p>
  <p class="para">
   Quand <span class="function"><strong>file_get_contents()</strong></span> est appelé sur un dossier,
   une erreur de niveau <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> est générée sur Windows,
   et à partir de PHP 7.4 sur les autres systèmes d&#039;exploitation aussi.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.file-get-contents-changelog">
  <h3 class="title">Historique</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        <code class="parameter">length</code> est désormais nullable.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        Ajout du support pour des positions <code class="parameter">offset</code> négatives.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.file-get-contents-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Lit et affiche le code HTML d&#039;un site Web</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$homepage </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$homepage</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-2">
    <p><strong>Exemple #2 Recherche un fichier dans le include_path</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Si le typage strict est activé c.-à-d. declare(strict_types=1);<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./people.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Sinon<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./people.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">FILE_USE_INCLUDE_PATH</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-3">
    <p><strong>Exemple #3 Lit une section d&#039;un fichier</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Lit 14 caractères à partir du 21ème caractère<br /></span><span style="color: #0000BB">$section </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./people.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">20</span><span style="color: #007700">, </span><span style="color: #0000BB">14</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$section</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Résultat de l&#039;exemple ci-dessus est similaire à :</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">string(14) &quot;lle Bjori Ro&quot;</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-4">
    <p><strong>Exemple #4 Utilisation des contextes de flux</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Création d'un flux<br /></span><span style="color: #0000BB">$opts </span><span style="color: #007700">= [<br />  </span><span style="color: #DD0000">'http' </span><span style="color: #007700">=&gt; [<br />    </span><span style="color: #DD0000">'method' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"GET"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'header' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Accept-language: en\r\n" </span><span style="color: #007700">.<br />                </span><span style="color: #DD0000">"Cookie: foo=bar"</span><span style="color: #007700">,<br />  ]<br />];<br /><br /></span><span style="color: #0000BB">$context </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Accès à un fichier HTTP avec les entêtes HTTP indiqués ci-dessus<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.file-get-contents-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">
 Cette fonction gère les chaînes binaires.
</span></p></blockquote>
  <div class="tip"><strong class="tip">Astuce</strong><p class="simpara">
 Vous pouvez utiliser une URL comme nom de fichier avec cette fonction, si le
 <a href="filesystem.configuration.php#ini.allow-url-fopen" class="link">gestionnaire fopen</a> a été activé. Voyez <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>
 pour plus de détails sur la façon de spécifier le nom du fichier. Reportez-vous aux
 <a href="wrappers.php" class="xref">Liste des protocoles et des gestionnaires support&eacute;s</a> pour plus d&#039;informations sur les capacités des différents gestionnaires,
 les notes sur leur utilisation, ainsi que les informations sur les variables prédéfinies qu&#039;elles fournissent.
</p></div>
  <div class="warning"><strong class="warning">Avertissement</strong><p class="para">
 Lorsque SSL est utilisé, le serveur IIS de Microsoft violera le protocole en fermant la connexion sans
 envoyer un indicateur <code class="literal">close_notify</code>. PHP le reportera en tant que &quot;SSL: Fatal Protocol Error&quot;
 quand vous arrivez à la fin des données. Pour contourner ce le niveau de la directive
 <a href="errorfunc.configuration.php#ini.error-reporting" class="link">error_reporting</a> doit être baissée pour ne pas inclure les avertissements.
 PHP peut détecter automatiquement les serveur IIS bogué lors de l&#039;ouverture
 du flux en utilisant <code class="literal">https://</code> et supprimera l&#039;avertissement.
 Lors de l&#039;utilisation de <span class="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span> pour créer un socket <code class="literal">ssl://</code>,
 c&#039;est au développeur de détecter et supprimer l&#039;avertissement.
</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.file-get-contents-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.file.php" class="function" rel="rdfs-seeAlso">file()</a> - Lit le fichier et renvoie le r&eacute;sultat dans un tableau</span></li>
    <li><span class="function"><a href="function.fgets.php" class="function" rel="rdfs-seeAlso">fgets()</a> - R&eacute;cup&egrave;re la ligne courante &agrave; partir de l'emplacement du pointeur sur fichier</span></li>
    <li><span class="function"><a href="function.fread.php" class="function" rel="rdfs-seeAlso">fread()</a> - Lecture du fichier en mode binaire</span></li>
    <li><span class="function"><a href="function.readfile.php" class="function" rel="rdfs-seeAlso">readfile()</a> - Affiche un fichier</span></li>
    <li><span class="function"><a href="function.file-put-contents.php" class="function" rel="rdfs-seeAlso">file_put_contents()</a> - &Eacute;crit des donn&eacute;es dans un fichier</span></li>
    <li><span class="function"><a href="function.stream-get-contents.php" class="function" rel="rdfs-seeAlso">stream_get_contents()</a> - Lit tout un flux dans une cha&icirc;ne</span></li>
    <li><span class="function"><a href="function.stream-context-create.php" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - Cr&eacute;e un contexte de flux</span></li>
    <li><a href="reserved.variables.httpresponseheader.php" class="link">$http_response_header</a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>