<?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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.fwrite.php',
    1 => 'fwrite',
    2 => 'Binary-safe file write',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Функції Файлової Системи',
  ),
  'prev' => 
  array (
    0 => 'function.ftruncate.php',
    1 => 'ftruncate',
  ),
  'next' => 
  array (
    0 => 'function.glob.php',
    1 => 'glob',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    '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">Binary-safe file write</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fwrite-description">
  <h3 class="title">Опис</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> writes the contents of
   <code class="parameter">data</code> to the file stream pointed to by
   <code class="parameter">stream</code>. 
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fwrite-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">stream</code></dt>
     <dd>
      <p class="para">Вказівник файлової системи типу
<span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>, якого повертає функція
<span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>.</p>
     </dd>
    
    
     <dt><code class="parameter">data</code></dt>
     <dd>
      <p class="para">
       The string that is to be written.
      </p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       If <code class="parameter">length</code> is an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>, writing will stop
       after <code class="parameter">length</code> bytes have been written or the
       end of <code class="parameter">data</code> is reached, whichever comes first.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fwrite-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="simpara">
   <span class="function"><strong>fwrite()</strong></span> returns the number of bytes
   written,  або <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> в разі помилки.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.fwrite-errors">
  <h3 class="title">Помилки/виключення</h3>
  <p class="para">
   <span class="function"><strong>fwrite()</strong></span> raises <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.fwrite-changelog">
  <h3 class="title">Журнал змін</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Версія</th>
      <th>Опис</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">length</code> is nullable now.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.fwrite-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 A simple <span class="function"><strong>fwrite()</strong></span> example</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">"Add this to the file\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Let's make sure the file exists and is writable first.<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">// In our example we're opening $filename in append mode.<br />    // The file pointer is at the bottom of the file hence<br />    // that's where $somecontent will go when we fwrite() it.<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 />         echo </span><span style="color: #DD0000">"Cannot open file (</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">)"</span><span style="color: #007700">;<br />         exit;<br />    }<br /><br />    </span><span style="color: #FF8000">// Write $somecontent to our opened file.<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 />        echo </span><span style="color: #DD0000">"Cannot write to file (</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">)"</span><span style="color: #007700">;<br />        exit;<br />    }<br /><br />    echo </span><span style="color: #DD0000">"Success, wrote (</span><span style="color: #0000BB">$somecontent</span><span style="color: #DD0000">) to file (</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">)"</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 />    echo </span><span style="color: #DD0000">"The file </span><span style="color: #0000BB">$filename</span><span style="color: #DD0000"> is not writable"</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">Примітки</h3>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    Writing to a network stream may end before the whole string is written.
    Return value of <span class="function"><strong>fwrite()</strong></span> may be checked:
    <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">Зауваження</strong>: 
   <p class="para">
    On systems which differentiate between binary and text files
    (i.e. Windows) the file must be opened with &#039;b&#039; included in
    <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> mode parameter.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    If <code class="parameter">stream</code> was <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>ed in
    append mode, <span class="function"><strong>fwrite()</strong></span>s are atomic (unless the size of
    <code class="parameter">data</code> exceeds the filesystem&#039;s block size, on some
    platforms, and as long as the file is on a local filesystem). That is,
    there is no need to <span class="function"><a href="function.flock.php" class="function">flock()</a></span> a resource before calling
    <span class="function"><strong>fwrite()</strong></span>; all of the data will be written without
    interruption.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    If writing twice to the file pointer, then the data will be appended to
    the end of the file content:
    <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">// the content of 'data.txt' is now 123 and not 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">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.fread.php" class="function" rel="rdfs-seeAlso">fread()</a> - Binary-safe file read</span></li>
    <li><span class="function"><a href="function.fopen.php" class="function" rel="rdfs-seeAlso">fopen()</a> - Opens file or URL</span></li>
    <li><span class="function"><a href="function.fsockopen.php" class="function" rel="rdfs-seeAlso">fsockopen()</a> - Open Internet or Unix domain socket connection</span></li>
    <li><span class="function"><a href="function.popen.php" class="function" rel="rdfs-seeAlso">popen()</a> - Opens process file pointer</span></li>
    <li><span class="function"><a href="function.file-get-contents.php" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - Читає вміст файла в рядку</span></li>
    <li><span class="function"><a href="function.pack.php" class="function" rel="rdfs-seeAlso">pack()</a> - Pack data into binary string</span></li>
   </ul>
  </p>
 </div>


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