<?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 => 'it',
  ),
  'this' => 
  array (
    0 => 'function.file-put-contents.php',
    1 => 'file_put_contents',
    2 => 'Write data to a file',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Filesystem Funzioni',
  ),
  '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' => 'en',
    '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">Write data to a file</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.file-put-contents-description">
  <h3 class="title">Descrizione</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"> 
   This function is identical to calling <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> and <span class="function"><a href="function.fclose.php" class="function">fclose()</a></span> successively
   to write data to a file.
  </p>
  <p class="para">
   If <code class="parameter">filename</code> does not exist, the file is created.
   Otherwise, the existing file is overwritten, unless the 
   <strong><code><a href="filesystem.constants.php#constant.file-append">FILE_APPEND</a></code></strong> flag is set.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.file-put-contents-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">filename</code></dt>
     <dd>
      <p class="para">
       Path to the file where to write the data.
      </p>
     </dd>
    
    
     <dt><code class="parameter">data</code></dt>
     <dd>
      <p class="para">
       The data to write. Can be either a <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, an
       <span class="type"><a href="language.types.array.php" class="type array">array</a></span> or a <span class="type">stream</span> resource.
      </p>
      <p class="para">
       If <code class="parameter">data</code> is a <span class="type">stream</span> resource, the
       remaining buffer of that stream will be copied to the specified file.
       This is similar with using <span class="function"><a href="function.stream-copy-to-stream.php" class="function">stream_copy_to_stream()</a></span>.
      </p>
      <p class="para">
       You can also specify the <code class="parameter">data</code> parameter as a single
       dimension array. This is equivalent to
       <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">
       The value of <code class="parameter">flags</code> can be any combination of 
       the following flags, joined with the binary OR (<code class="literal">|</code>)
       operator.
      </p>
      <p class="para">
       <table class="doctable table">
        <caption><strong>Available flags</strong></caption>
        
         <thead>
          <tr>
           <th>Flag</th>
           <th>Description</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>
            Search for <code class="parameter">filename</code> in the include directory.
            See <a href="ini.core.php#ini.include-path" class="link">include_path</a> for more
            information.
           </td>
          </tr>

          <tr>
           <td>
            <strong><code><a href="filesystem.constants.php#constant.file-append">FILE_APPEND</a></code></strong>
           </td>
           <td>
            If file <code class="parameter">filename</code> already exists, append 
            the data to the file instead of overwriting it.
           </td>
          </tr>

          <tr>
           <td>
            <strong><code><a href="filesystem.constants.php#constant.lock-ex">LOCK_EX</a></code></strong>
           </td>
           <td>
            Acquire an exclusive lock on the file while proceeding to the 
            writing. In other words, a <span class="function"><a href="function.flock.php" class="function">flock()</a></span> call happens
            between the <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> call and the 
            <span class="function"><a href="function.fwrite.php" class="function">fwrite()</a></span> call. This is not identical to an 
            <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> call with mode &quot;x&quot;.
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
      <p class="para">
       A valid context resource created with 
       <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">Valori restituiti</h3>
  <p class="para">
   This function returns the number of bytes that were written to the file, or
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
  <div class="warning"><strong class="warning">Avviso</strong><p class="simpara">Questa funzione può
restituire il Booleano <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, ma può anche restituire un valore non-Booleano valutato
come <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>. Fare riferimento alla sezione <a href="language.types.boolean.php" class="link">Booleans</a> per maggiori 
informazioni. Usare <a href="language.operators.comparison.php" class="link">l&#039;operatore ===
</a> per controllare il valore restituito da questa 
funzione.</p></div>
 </div>


 <div class="refsect1 examples" id="refsect1-function.file-put-contents-examples">
  <h3 class="title">Esempi</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Simple usage example</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">// Open the file to get existing content<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">// Append a new person to the file<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">// Write the contents back to the file<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>Example #2 Using 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">// The new person to add to the file<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">// Write the contents to the file, <br />// using the FILE_APPEND flag to append the content to the end of the file<br />// and the LOCK_EX flag to prevent anyone else writing to the file at the same time<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">Note</h3>
  <blockquote class="note"><p><strong class="note">Nota</strong>: <span class="simpara">Questa funzione è
 binary-safe (gestisce correttamente i file binari)</span></p></blockquote>
  <div class="tip"><strong class="tip">Suggerimento</strong><p class="simpara">È possibile utilizzare una URL come un
nome di file con questa funzione se <a href="filesystem.configuration.php#ini.allow-url-fopen" class="link">fopen wrappers</a> è stata abilitata.
Vedere <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> per maggiori informazioni su come specificare 
i nomi di file. Vedere <a href="wrappers.php" class="xref">Supported Protocols and Wrappers</a> per i link verso le informazioni
sulle capacità dei vari wrapper, note sul loro uso,
informazioni sulle variabili predefinite che
forniscono.</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.file-put-contents-seealso">
  <h3 class="title">Vedere anche:</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.fopen.php" class="function" rel="rdfs-seeAlso">fopen()</a> - Apre un file o un URL</span></li>
    <li><span class="function"><a href="function.fwrite.php" class="function" rel="rdfs-seeAlso">fwrite()</a> - Scrittura su file binary-safe</span></li>
    <li><span class="function"><a href="function.file-get-contents.php" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - Legge un file all'interno di una stringa</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); ?>