<?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.fread.php',
    1 => 'fread',
    2 => 'Liest Bin&auml;rdaten aus einer Datei',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Dateisystem',
  ),
  'prev' => 
  array (
    0 => 'function.fputs.php',
    1 => 'fputs',
  ),
  'next' => 
  array (
    0 => 'function.fscanf.php',
    1 => 'fscanf',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    'path' => 'reference/filesystem/functions/fread.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.fread" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fread</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">fread</span> &mdash; <span class="dc-title">Liest Binärdaten aus einer Datei</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.fread-description">
  <h3 class="title">Beschreibung</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fread</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> <code class="parameter">$stream</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$length</code></span>): <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">
   <span class="function"><strong>fread()</strong></span> liest bis zu
   <code class="parameter">length</code> Bytes vom Dateizeiger
   <code class="parameter">stream</code>. Das Lesen wird beendet, sobald
   eine der folgenden Bedingungen eintritt:
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      <code class="parameter">length</code> Bytes wurden gelesen
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      Das Dateiende (EOF, end of file) wurde erreicht
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      Ein Paket wird verfügbar oder eine
      <a href="function.socket-set-timeout.php" class="link">Socket-Zeitüberschreitung</a>
      tritt auf (Bei Netzwerk-Streams)
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      Falls dieser Stream beim Lesen gepuffert wird und keine reguläre Datei
      repräsentiert, wird maximal einmal ein Stück bis zur maximalen Chunkgröße
      (normalerweise 8192 Bytes) gelesen. Abhängig von den bisher gepufferten
      Daten können die zurückgelieferten Daten größer als die Chunkgröße sein.
     </span>
    </li>
   </ul>
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fread-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">stream</code></dt>
     <dd>
      <p class="para">Eine Dateisystemressource (<span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>),
wie sie in der Regel von <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> zurückgegeben
wird.</p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       Bis zu <code class="parameter">length</code> Bytes lesen.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fread-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="para">
   Gibt die ausgelesene Zeichenkette zurück. Bei einem Fehler wird <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> zurückgegeben.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.fread-examples">
  <h3 class="title">Beispiele</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Beispiel #1 Ein einfaches <span class="function"><strong>fread()</strong></span>-Beispiel</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">// liest den Inhalt einer Datei in einen String<br /></span><span style="color: #0000BB">$filename </span><span style="color: #007700">= </span><span style="color: #DD0000">"/usr/local/etwas.txt"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">, </span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Beispiel #2 Ein <span class="function"><strong>fread()</strong></span>-Beispiel mit einer Binärdatei</strong></p>
    <div class="warning"><strong class="warning">Warnung</strong>
     <p class="para">
      Auf Systemen, welche zwischen Binär- und Textdateien (z. B. Windows)
      unterscheiden, muss die Datei mit &#039;b&#039; im <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>
      Mode-Parameter geöffnet werden.
     </p>
    </div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$filename </span><span style="color: #007700">= </span><span style="color: #DD0000">"c:\\files\\somepic.gif"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #DD0000">"rb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">, </span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Beispiel #3 <span class="function"><strong>fread()</strong></span>-Beispiele für entfernte Dateien</strong></p>
    <div class="warning"><strong class="warning">Warnung</strong>
     <p class="para">
      Wenn aus etwas gelesen werden soll, was keine normale lokale Datei ist,
      wie etwa Streams von
      <a href="features.remote-files.php" class="link">entfernten Dateien</a> oder von
      <span class="function"><a href="function.popen.php" class="function">popen()</a></span> und <span class="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span>, so wird
      das Lesen beendet sobald ein Paket verfügbar ist. Das bedeutet, dass man
      die Daten in Stücken sammeln muss. Dies wird im unteren Beispiel
      gezeigt.
     </p>
    </div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$handle </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">"rb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$handle </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">"rb"</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">FALSE </span><span style="color: #007700">=== </span><span style="color: #0000BB">$handle</span><span style="color: #007700">) {<br />    exit(</span><span style="color: #DD0000">"Konnte Stream von URL nicht öffnen"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br />while (!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">)) {<br />  </span><span style="color: #0000BB">$contents </span><span style="color: #007700">.= </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">, </span><span style="color: #0000BB">8192</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</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.fread-notes">
  <h3 class="title">Anmerkungen</h3>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Wenn man nur den Inhalt einer Datei in eine Zeichenkette lesen will,
    so sollte man <span class="function"><a href="function.file-get-contents.php" class="function">file_get_contents()</a></span> verwenden, da dies
    eine bessere Performance hat als der obige Code.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Beachten Sie, dass <span class="function"><strong>fread()</strong></span> an der aktuellen Position
    im Dateizeiger zu lesen beginnt. Verwenden Sie <span class="function"><a href="function.ftell.php" class="function">ftell()</a></span>,
    um herauszufinden wo dieser Zeiger steht und die Funktion
    <span class="function"><a href="function.rewind.php" class="function">rewind()</a></span>, um den Zeiger zurückzustellen.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.fread-seealso">
  <h3 class="title">Siehe auch</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.fwrite.php" class="function" rel="rdfs-seeAlso">fwrite()</a> - Bin&auml;r-sicheres Dateischreiben</span></li>
    <li><span class="function"><a href="function.fopen.php" class="function" rel="rdfs-seeAlso">fopen()</a> - &Ouml;ffnet eine Datei oder URL</span></li>
    <li><span class="function"><a href="function.fsockopen.php" class="function" rel="rdfs-seeAlso">fsockopen()</a> - Stellt eine Internet- oder Unix-Domain-Socket-Verbindung her</span></li>
    <li><span class="function"><a href="function.popen.php" class="function" rel="rdfs-seeAlso">popen()</a> - &Ouml;ffnet einen Dateizeiger f&uuml;r einen Prozess</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.fgetss.php" class="function" rel="rdfs-seeAlso">fgetss()</a> - Liest eine Zeile von der Position des Dateizeigers und entfernt HTML Tags.</span></li>
    <li><span class="function"><a href="function.fscanf.php" class="function" rel="rdfs-seeAlso">fscanf()</a> - Interpretiert den Input einer Datei entsprechend einem
   angegebenen Format</span></li>
    <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.fpassthru.php" class="function" rel="rdfs-seeAlso">fpassthru()</a> - Gibt alle verbleibenden Daten eines Dateizeigers aus</span></li>
    <li><span class="function"><a href="function.fseek.php" class="function" rel="rdfs-seeAlso">fseek()</a> - Positioniert den Dateizeiger</span></li>
    <li><span class="function"><a href="function.ftell.php" class="function" rel="rdfs-seeAlso">ftell()</a> - Ermittelt die aktuelle Position des Dateizeigers</span></li>
    <li><span class="function"><a href="function.rewind.php" class="function" rel="rdfs-seeAlso">rewind()</a> - Setzt die Position eines Dateizeigers auf den Anfang</span></li>
    <li><span class="function"><a href="function.unpack.php" class="function" rel="rdfs-seeAlso">unpack()</a> - Entpackt die Daten einer bin&auml;re Zeichenkette</span></li>
   </ul>
  </p>
 </div>

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