<?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-put-contents.php',
    1 => 'file_put_contents',
    2 => 'Schreibt Daten in eine Datei',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Dateisystem',
  ),
  'prev' => 
  array (
    0 => 'function.file-get-contents.php',
    1 => 'file_get_contents',
  ),
  'next' => 
  array (
    0 => 'function.fileatime.php',
    1 => 'fileatime',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    'path' => 'reference/filesystem/functions/file-put-contents.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.file-put-contents" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">file_put_contents</h1> 
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">file_put_contents</span> &mdash; <span class="dc-title">Schreibt Daten in eine Datei</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.file-put-contents-description">
  <h3 class="title">Beschreibung</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>file_put_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.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$data</code></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">$flags</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.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>): <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="para rdfs-comment"> 
   Diese Funktion entspricht <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>, 
   <span class="function"><a href="function.fwrite.php" class="function">fwrite()</a></span> und <span class="function"><a href="function.fclose.php" class="function">fclose()</a></span>, um Daten in eine Datei zu schreiben.
  </p>
  <p class="para">
   Wenn <code class="parameter">filename</code> nicht existiert, wird die Datei erstellt.
   Sonst wird die existierende Datei überschrieben, es sei denn 
   <strong><code><a href="filesystem.constants.php#constant.file-append">FILE_APPEND</a></code></strong> ist gesetzt.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.file-put-contents-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">filename</code></dt>
     <dd>
      <p class="para">
       Pfad zur Datei in die die Daten geschrieben werden sollen.
      </p>
     </dd>
    
    
     <dt><code class="parameter">data</code></dt>
     <dd>
      <p class="para">
       Die zu schreibenden Daten. Kann entweder <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, oder
       <span class="type"><a href="language.types.array.php" class="type array">array</a></span> sowie <span class="type">stream</span> sein.
      </p>
      <p class="para">
       Wenn <code class="parameter">data</code> eine <span class="type">stream</span>-Quelle ist, wird 
       der verbleibende Puffer des Streams in die festgelegte Datei kopiert.
       Dies ähnelt der Funktion <span class="function"><a href="function.stream-copy-to-stream.php" class="function">stream_copy_to_stream()</a></span>.
      </p>
      <p class="para">
       Sie können auch den Parameter <code class="parameter">data</code> als eindimensionales Array angeben.
       Dies ist äquivalent zu
       <code class="literal">file_put_contents($filename, implode(&#039;&#039;, $array))</code>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       Der Wert von <code class="parameter">flags</code> kann eine Kombination der folgenden Flags sein, verbunden mit dem binären OR(<code class="literal">|</code>)
       -Operator.
      </p>
      <p class="para">
       <table class="doctable table">
        <caption><strong>verfügbare Flags</strong></caption>
        
         <thead>
          <tr>
           <th>Flag</th>
           <th>Beschreibung</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td>
            <strong><code><a href="filesystem.constants.php#constant.file-use-include-path">FILE_USE_INCLUDE_PATH</a></code></strong>
           </td>
           <td>
            Sucht nach <code class="parameter">filename</code> im enthaltenen Verzeichnis.
            Siehe <a href="ini.core.php#ini.include-path" class="link">include_path</a> für mehr Information.
           </td>
          </tr>

          <tr>
           <td>
            <strong><code><a href="filesystem.constants.php#constant.file-append">FILE_APPEND</a></code></strong>
           </td>
           <td>
            Falls die Datei <code class="parameter">filename</code> bereits existiert, füge die Daten
            an die Datei an anstatt diese zu überschreiben.
           </td>
          </tr>

          <tr>
           <td>
            <strong><code><a href="filesystem.constants.php#constant.lock-ex">LOCK_EX</a></code></strong>
           </td>
           <td>
            Erwirbt exklusive Schreibrechte auf die Datei während mit dem
            Schreiben fortgefahren wird. Anders ausgedrückt erfolgt ein
            <span class="function"><a href="function.flock.php" class="function">flock()</a></span> Aufruf zwischen dem
            <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> Aufruf und dem
            <span class="function"><a href="function.fwrite.php" class="function">fwrite()</a></span> Aufruf. Das ist nicht das gleiche wie
            ein <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> Aufruf mit Modus &quot;x&quot;.
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
      <p class="para">
       Eine gültige Kontakt-Ressource erstellt mit der Funktion 
       <span class="function"><a href="function.stream-context-create.php" class="function">stream_context_create()</a></span>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.file-put-contents-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="para">
   Diese Funktion gibt die Anzahl der geschriebenen Bytes oder
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> bei einem Fehler zurück.
  </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 examples" id="refsect1-function.file-put-contents-examples">
  <h3 class="title">Beispiele</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Beispiel #1 Einfaches Verwendungsbeispiel</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: #DD0000">'people.txt'</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// Öffnet die Datei, um den vorhandenen Inhalt zu laden<br /></span><span style="color: #0000BB">$current </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Fügt eine neue Person zur Datei hinzu<br /></span><span style="color: #0000BB">$current </span><span style="color: #007700">.= </span><span style="color: #DD0000">"John Smith\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// Schreibt den Inhalt in die Datei zurück<br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">, </span><span style="color: #0000BB">$current</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 Verwendung der Flags</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: #DD0000">'people.txt'</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//Die neue Person, die zur Datei hinzugefügt werden soll<br /></span><span style="color: #0000BB">$person </span><span style="color: #007700">= </span><span style="color: #DD0000">"John Smith\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// Schreibt den Inhalt in die Datei<br />// unter Verwendung des Flags FILE_APPEND, um den Inhalt an das Ende der Datei anzufügen<br />// und das Flag LOCK_EX, um ein Schreiben in die selbe Datei zur gleichen Zeit zu verhindern<br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">, </span><span style="color: #0000BB">$person</span><span style="color: #007700">, </span><span style="color: #0000BB">FILE_APPEND </span><span style="color: #007700">| </span><span style="color: #0000BB">LOCK_EX</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-put-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>


 <div class="refsect1 seealso" id="refsect1-function.file-put-contents-seealso">
  <h3 class="title">Siehe auch</h3>
  <p class="para">
   <ul class="simplelist">
    <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.fwrite.php" class="function" rel="rdfs-seeAlso">fwrite()</a> - Bin&auml;r-sicheres Dateischreiben</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.stream-context-create.php" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - Creates a stream context</span></li>
   </ul>
  </p>
 </div>


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