<?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 => 'de',
  ),
  'this' => 
  array (
    0 => 'function.file-get-contents.php',
    1 => 'file_get_contents',
    2 => 'Liest die gesamte Datei in einen String',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Dateisystem',
  ),
  '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' => 'de',
    '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">Liest die gesamte Datei in einen String</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.file-get-contents-description">
  <h3 class="title">Beschreibung</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">
   Diese Funktion ist mit <span class="function"><a href="function.file.php" class="function">file()</a></span> identisch,
   außer dass <span class="function"><strong>file_get_contents()</strong></span> die Datei in
   einem String zurückgibt, beginnend am angebenen <code class="parameter">offset</code>
   über bis zu <code class="parameter">length</code> Bytes. Im Fehlerfall gibt
   <span class="function"><strong>file_get_contents()</strong></span> <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> zurück.
  </p>
  <p class="para">
   <span class="function"><strong>file_get_contents()</strong></span> ist der empfohlene Weg, um den
   Inhalt einer Datei in einen String zu lesen. Es werden Techniken zur
   Speicherabbildung genutzt, um die Performance zu erhöhen, falls das
   Betriebssystem dies unterstützt.
  </p>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Falls Sie einen URI mit speziellen Zeichen, wie z. B. Leerzeichen, öffnen,
    müssen Sie den URI mittels <span class="function"><a href="function.urlencode.php" class="function">urlencode()</a></span> enkodieren.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.file-get-contents-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">filename</code></dt>
     <dd>
      <p class="para">
       Name der zu lesenden Datei.
      </p>
     </dd>
    
    
     <dt><code class="parameter">use_include_path</code></dt>
     <dd>
      <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
       <p class="para">
        Die Konstante
        <strong><code><a href="filesystem.constants.php#constant.file-use-include-path">FILE_USE_INCLUDE_PATH</a></code></strong> kann genutzt werden, um eine
        Suche im <a href="ini.core.php#ini.include-path" class="link">include path</a>
        auszulösen.
        Dies ist nicht möglich, wenn <a href="language.types.declarations.php#language.types.declarations.strict" class="link">strenge
        Typisierung</a> aktiviert wurde, da
        <strong><code><a href="filesystem.constants.php#constant.file-use-include-path">FILE_USE_INCLUDE_PATH</a></code></strong> ein
        <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> ist. Statt dessen ist <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> zu verwenden.
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
      <p class="para">
       Eine gültige Context-Ressource, die mit
       <span class="function"><a href="function.stream-context-create.php" class="function">stream_context_create()</a></span> erstellt wurde. Falls Sie
       keinen eigenen Context benötigen, können Sie diesen Parameter mit
       <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> überspringen.
      </p>
     </dd>
    
    
     <dt><code class="parameter">offset</code></dt>
     <dd>
      <p class="para">
       Die Position, an der das Lesen im Originalstream beginnt.
       Negative Offsets zählen vom Ende des Streams an.
      </p>
      <p class="para">
       Das Springen an bestimmte Positionen (<code class="parameter">offset</code>)
       wird nicht für entfernte Dateien unterstützt. Für kleine Offsets kann
       der Sprung funktionieren, allerdings ist dies nicht vorhersagbar,
       weil hierbei auf dem gepufferten Stream gearbeitet wird.
      </p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       Maximale Länge der zu lesenden Daten. Standardmäßig wird solange
       gelesen bis das Ende der Datei erreicht wird. Beachten Sie, dass
       dieser Parameter auf den Stream angewendet wird, der durch die
       Filter verarbeitet wird.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.file-get-contents-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="para">
   Die Funktion gibt die gelesenen Daten zurück. Bei einem Fehler wird <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> zurückgegeben.
  </p>
  <div class="warning"><strong class="warning">Warnung</strong><p class="simpara">Diese Funktion kann sowohl das
boolsche <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> zurückliefern, als auch einen nicht-boolschen Wert, welcher zu <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> ausgewertet wird.
Weitere Informationen entnehmen Sie bitte dem Abschnitt über die
<a href="language.types.boolean.php" class="link"> boolschen Typen</a>. Benutzen Sie deshalb
<a href="language.operators.comparison.php" class="link">den === Operator</a>,
um den Rückgabewert dieser Funktion zu überprüfen.</p></div>
 </div>


 <div class="refsect1 errors" id="refsect1-function.file-get-contents-errors">
  <h3 class="title">Fehler/Exceptions</h3>
  <p class="para">
   Ein Fehler der Stufe <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> wird generiert, wenn
   <code class="parameter">filename</code> nicht gefunden werden kann,
   <code class="parameter">length</code> kleiner als Null ist oder wenn das Aufsuchen des angegebenen
   <code class="parameter">offset</code> im Streams fehlschlägt.
  </p>
  <p class="para">
   Wenn <span class="function"><strong>file_get_contents()</strong></span> für ein Verzeichnis aufgerufen wird,
   wird unter Windows und ab PHP 7.4 auch auf anderen Betriebssystemen ein Fehler der Ebene <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> generiert.
  </p>
 </div>


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

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        <code class="parameter">length</code> ist jetzt nullbar.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        Unterstützung für negative <code class="parameter">offset</code>s wurde hinzugefügt.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.file-get-contents-examples">
  <h3 class="title">Beispiele</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Beispiel #1 Holen und Ausgeben des Quelltextes der Startseite einer Webseite</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>Beispiel #2 Suche im 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">// Wenn strikte Typisierung aktiviert wurde, d. h. 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">'./leute.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Andernfalls<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">'./leute.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>Beispiel #3 Lesen einen Abschnitts einer Datei</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">// Lese 14 Zeichen, beginnend mit dem 21. Zeichen<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">'./leute.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>Das oben gezeigte Beispiel erzeugt
eine ähnliche Ausgabe wie:</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>Beispiel #4 Nutzung von Stream-Contexten</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">// Erzeugen eines Streams<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">// Öffnen der Datei mit den oben definierten HTTP-Headern<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">Anmerkungen</h3>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: <span class="simpara">Diese Funktion ist
binärsicher.</span></p></blockquote>
  <div class="tip"><strong class="tip">Tipp</strong><p class="simpara">Wenn
<a href="filesystem.configuration.php#ini.allow-url-fopen" class="link">fopen wrappers</a> aktiviert ist, kann
mit dieser Funktion eine URL als Dateiname verwendet werden. Mehr Details dazu,
wie der Dateiname angeben werden muss, sind bei <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> zu
finden. Eine Liste der unterstützten URL-Protokolle, die Fähigkeiten der
verschiedenen Wrapper, Hinweise zu deren Verwendung und Informationen zu den
eventuell vorhandenen vordefinierten Variablen sind unter
<a href="wrappers.php" class="xref">Unterst&uuml;tzte Protokolle und Wrapper</a> zu finden.</p></div>
  <div class="warning"><strong class="warning">Warnung</strong><p class="para">Bei SSL-Verbindungen zusammen
mit Microsoft IIS hält sich dieser Webserver nicht an das Protokoll und
schließt die Verbindung ohne ein <code class="literal">close_notify</code> zu senden.
PHP quittiert dieses Fehlverhalten mit &quot;SSL: Fatal Protocol Error&quot;, wenn das
Ende der Daten erreicht ist. Eine mögliche Lösung besteht darin, den Level von
<a href="errorfunc.configuration.php#ini.error-reporting" class="link">error_reporting</a> herabzusetzten und
Warnings auszuschließen. PHP kann fehlerhafte IIS-Serversoftware
erkennen, wenn Sie einen Stream mit dem <code class="literal">https://</code>-Wrapper öffnen,
und unterdrückt die Warnung für Sie. Falls Sie <span class="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span>
benutzen, um einen <code class="literal">ssl://</code>-Socket zu öffnen, müssen Sie selbst
dafür Sorge tragen, die Warnung zu erkennen und diese zu unterdrücken.</p>
</div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.file-get-contents-seealso">
  <h3 class="title">Siehe auch</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.file.php" class="function" rel="rdfs-seeAlso">file()</a> - Liest eine komplette Datei in ein Array</span></li>
    <li><span class="function"><a href="function.fgets.php" class="function" rel="rdfs-seeAlso">fgets()</a> - Liest die Zeile von der Position des Dateizeigers</span></li>
    <li><span class="function"><a href="function.fread.php" class="function" rel="rdfs-seeAlso">fread()</a> - Liest Bin&auml;rdaten aus einer Datei</span></li>
    <li><span class="function"><a href="function.readfile.php" class="function" rel="rdfs-seeAlso">readfile()</a> - Gibt eine Datei aus</span></li>
    <li><span class="function"><a href="function.file-put-contents.php" class="function" rel="rdfs-seeAlso">file_put_contents()</a> - Schreibt Daten in eine Datei</span></li>
    <li><span class="function"><a href="function.stream-get-contents.php" class="function" rel="rdfs-seeAlso">stream_get_contents()</a> - Reads remainder of a stream into a string</span></li>
    <li><span class="function"><a href="function.stream-context-create.php" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - Creates a stream context</span></li>
    <li><a href="reserved.variables.httpresponseheader.php" class="link">$http_response_header</a></li>
   </ul>
  </p>
 </div>


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