<?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.fwrite.php',
    1 => 'fwrite',
    2 => 'Bin&auml;r-sicheres Dateischreiben',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Dateisystem',
  ),
  'prev' => 
  array (
    0 => 'function.ftruncate.php',
    1 => 'ftruncate',
  ),
  'next' => 
  array (
    0 => 'function.glob.php',
    1 => 'glob',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    'path' => 'reference/filesystem/functions/fwrite.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.fwrite" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fwrite</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">fwrite</span> &mdash; <span class="dc-title">Binär-sicheres Dateischreiben</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.fwrite-description">
  <h3 class="title">Beschreibung</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fwrite</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.string.php" class="type string">string</a></span> <code class="parameter">$data</code></span>, <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>): <span class="type"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="simpara">
   <span class="function"><strong>fwrite()</strong></span> schreibt den Inhalt der Zeichenkette
   <code class="parameter">data</code> in die Datei, auf welche der Dateizeiger
   <code class="parameter">stream</code> zeigt.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fwrite-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">data</code></dt>
     <dd>
      <p class="para">
       Die zu schreibende Zeichenkette.
      </p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       Wenn Parameter <code class="parameter">length</code> eine <a href="language.types.integer.php" class="link">Integer</a> ist, wird
       das Schreiben nach <code class="parameter">length</code> Bytes beendet, oder
       wenn das Ende von <code class="parameter">data</code> erreicht ist, je
       nachdem was eher eintritt.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fwrite-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="simpara">
   <span class="function"><strong>fwrite()</strong></span> gibt die Anzahl der geschriebenen Bytes 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 errors" id="refsect1-function.fwrite-errors">
  <h3 class="title">Fehler/Exceptions</h3>
  <p class="para">
   <span class="function"><strong>fwrite()</strong></span> erzeugt bei einem Fehler ein
   <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.fwrite-changelog">
  <h3 class="title">Changelog</h3>
  <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>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.fwrite-examples">
  <h3 class="title">Beispiele</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Beispiel #1 Ein einfaches Beispiel für <span class="function"><strong>fwrite()</strong></span></strong></p>
    <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">'test.txt'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$somecontent </span><span style="color: #007700">= </span><span style="color: #DD0000">"Füge dies der Datei hinzu\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Sichergehen, dass die Datei existiert und beschreibbar ist.<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">is_writable</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">)) {<br /><br />    </span><span style="color: #FF8000">// Wir öffnen $filename im Anfüge-Modus.<br />    // Der Dateizeiger befindet sich am Ende der Datei, und daher<br />    // wird später $somecontent mit fwrite() dorthin geschrieben.<br />    </span><span style="color: #007700">if (!</span><span style="color: #0000BB">$fp </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">"a"</span><span style="color: #007700">)) {<br />         print </span><span style="color: #DD0000">"Kann die Datei </span><span style="color: #0000BB">$filename</span><span style="color: #DD0000"> nicht öffnen"</span><span style="color: #007700">;<br />         exit;<br />    }<br /><br />    </span><span style="color: #FF8000">// Schreibe $somecontent in die geöffnete Datei.<br />    </span><span style="color: #007700">if (</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">$somecontent</span><span style="color: #007700">) === </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">) {<br />        print </span><span style="color: #DD0000">"Kann nicht in die Datei </span><span style="color: #0000BB">$filename</span><span style="color: #DD0000"> schreiben"</span><span style="color: #007700">;<br />        exit;<br />    }<br /><br />    print </span><span style="color: #DD0000">"Fertig, in die Datei </span><span style="color: #0000BB">$filename</span><span style="color: #DD0000"> wurde </span><span style="color: #0000BB">$somecontent</span><span style="color: #DD0000"> geschrieben"</span><span style="color: #007700">;<br /><br />    </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br />} else {<br />    print </span><span style="color: #DD0000">"Die Datei </span><span style="color: #0000BB">$filename</span><span style="color: #DD0000"> ist nicht schreibbar"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

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


 <div class="refsect1 notes" id="refsect1-function.fwrite-notes">
  <h3 class="title">Anmerkungen</h3>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Das Schreiben in einen Netzwerkstream kann enden, bevor die gesamte
    Zeichenkette geschrieben wurde. Der Rückgabewert von
    <span class="function"><strong>fwrite()</strong></span> kann wie folgt überprüft werden:
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">fwrite_stream</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">$string</span><span style="color: #007700">) {<br />    for (</span><span style="color: #0000BB">$written </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$written </span><span style="color: #007700">&lt; </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">); </span><span style="color: #0000BB">$written </span><span style="color: #007700">+= </span><span style="color: #0000BB">$fwrite</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$fwrite </span><span style="color: #007700">= </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">, </span><span style="color: #0000BB">$written</span><span style="color: #007700">));<br />        if (</span><span style="color: #0000BB">$fwrite </span><span style="color: #007700">=== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br />            return </span><span style="color: #0000BB">$written</span><span style="color: #007700">;<br />        }<br />    }<br />    return </span><span style="color: #0000BB">$written</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </p>
  </p></blockquote>

  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Auf Systemen, welche zwischen Binär- und Textdateien unterscheiden (z. B.
    Windows), muss die Datei mit dem <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>-Modus-Parameter
    &#039;b&#039; geöffnet werden.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Wurde <code class="parameter">stream</code> mit <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> im
    Anfügemodus geöffnet, ist das Schreiben mit <span class="function"><strong>fwrite()</strong></span>
    atomar (außer wenn die Länge von <code class="parameter">data</code> die
    Blockgröße des Dateisystems auf manchen Systemen überschreitet, und
    solange sich die Datei in einem lokalen Dateisystem befindet), d. h. es
    besteht keine Notwendigkeit, eine Ressource vor dem Aufruf von
    <span class="function"><strong>fwrite()</strong></span> mit <span class="function"><a href="function.flock.php" class="function">flock()</a></span> zu sperren;
    alle Daten werden ohne Unterbrechung geschrieben.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Wird zweimalig in den Dateizeiger geschrieben, werden die Daten an das
    Ende des Dateiinhalts angehängt:
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'data.txt'</span><span style="color: #007700">, </span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">'1'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">'23'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// der Inhalt von 'data.txt' ist nun 123 und nicht 23!<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

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


 <div class="refsect1 seealso" id="refsect1-function.fwrite-seealso">
  <h3 class="title">Siehe auch</h3>
  <p class="para">
   <ul class="simplelist">
    <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.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.file-get-contents.php" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - Liest die gesamte Datei in einen String</span></li>
    <li><span class="function"><a href="function.pack.php" class="function" rel="rdfs-seeAlso">pack()</a> - Packt Daten in eine bin&auml;re Zeichenkette</span></li>
   </ul>
  </p>
 </div>


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