<?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.file-put-contents.php',
    1 => 'file_put_contents',
    2 => 'Записує рядок у файл',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Функції Файлової Системи',
  ),
  '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' => 'uk',
    '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">Записує рядок у файл</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.file-put-contents-description">
  <h3 class="title">Опис</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"><a href="language.types.resource.php" class="type resource">resource</a></span> <code class="parameter">$context</code><span class="initializer"> = ?</span></span><br>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="para rdfs-comment"> 
   Ця функція є ідентичною до послідовних викликів <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> та <span class="function"><a href="function.fclose.php" class="function">fclose()</a></span> для
   записування даних у файл.
  </p>
  <p class="para">
   Якщо файлу (<code class="parameter">filename</code>) не існує, його буде створено.
   В іншому випадку, існуючий файл буде перезаписано, якщо не встановлено
   флаг <strong><code><a href="filesystem.constants.php#constant.file-append">FILE_APPEND</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.file-put-contents-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">filename</code></dt>
     <dd>
      <p class="para">
       Шлях до файлу, куди будуть записуватись дані.
      </p>
     </dd>
    
    
     <dt><code class="parameter">data</code></dt>
     <dd>
      <p class="para">
       Дані для запису. Може бути рядком (<span class="type"><a href="language.types.string.php" class="type string">string</a></span>), масивом
       (<span class="type"><a href="language.types.array.php" class="type array">array</a></span>) або ресурсом потоку (<span class="type">stream</span>).
      </p>
      <p class="para">
       Якщо дані (<code class="parameter">data</code>) є ресурсом потоку
       (<span class="type">stream</span>), залишок буфера цього потоку буде скопійовано до
     зазначеного файла.
       Це схоже на використання <span class="function"><a href="function.stream-copy-to-stream.php" class="function">stream_copy_to_stream()</a></span>.
      </p>
      <p class="para">
       Ви також можете зазначити параметр <code class="parameter">data</code> як
       як одномірний одиночний масив. Це буде еквівалентно до виклика
       <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">
       Значення параметра <code class="parameter">flags</code> може бути будь-якою
       комбінацією з наступних флагів, об&#039;єднаних бінарним оператором АБО
       (<code class="literal">|</code>).
      </p>
      <p class="para">
       <table class="doctable table">
        <caption><strong>Доступні флаги</strong></caption>
        
         <thead>
          <tr>
           <th>Флаг</th>
           <th>Опис</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>
            Пошук файла <code class="parameter">filename</code> в директоріях,
            що підключаються. Прогляньте <a href="ini.core.php#ini.include-path" class="link">include_path</a>
            для більш детальної інформації.
           </td>
          </tr>

          <tr>
           <td>
            <strong><code><a href="filesystem.constants.php#constant.file-append">FILE_APPEND</a></code></strong>
           </td>
           <td>
            Якщо файл <code class="parameter">filename</code> вже існує, додавати
            дані, замість перезаписування старого вмісту.
           </td>
          </tr>

          <tr>
           <td>
            <strong><code><a href="filesystem.constants.php#constant.lock-ex">LOCK_EX</a></code></strong>
           </td>
           <td>
            Отримати монопольне блокування поки йде процес записування.
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
      <p class="para">
       Коректний ресурс контента створений за допомогою функції
       <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">Значення, що повертаються</h3>
  <p class="para">
   Ця функція повертає кількість байтів, які будуть записані до файла, або
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> при невдачі.
  </p>
  <div class="warning"><strong class="warning">Увага</strong><p class="simpara">Ця функція може
повертати як логічне <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, так і не логічне значення, яке прирівнюється до
<strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>. Докладніше про це описано в розділі <a href="language.types.boolean.php" class="link">Логічні типи даних</a>. Для перевірки
значення, яке повертає ця функція, використовується <a href="language.operators.comparison.php" class="link">оператор ===</a>.</p></div>
 </div>


 <div class="refsect1 examples" id="refsect1-function.file-put-contents-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 Просте використання</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 /><br /></span><span style="color: #FF8000">// Відкрити файл для отримання існуючого вмісту<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 /><br /></span><span style="color: #FF8000">// Додати нову особу до файлу<br /></span><span style="color: #0000BB">$current </span><span style="color: #007700">.= </span><span style="color: #DD0000">"Коля Шкраб"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Вписати вміст назад у файл<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>Приклад #2 Використання флагів</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 /><br /></span><span style="color: #FF8000">// Додати нову особу до файлу<br /></span><span style="color: #0000BB">$person </span><span style="color: #007700">= </span><span style="color: #DD0000">"Коля Шкраб"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Запис до файлу з використанням флагу FILE_APPEND,<br />// щоб вміст додавався в кінець,<br />// та блокування файлу за допомогою флагу LOCK_EX, щоб недопустити одночасний запис ще кимось<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 changelog" id="refsect1-function.file-put-contents-changelog">
  <h3 class="title">Журнал змін</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Версія</th>
       <th>Опис</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.1.0</td>
       <td>
        Додано підтримку для <strong><code><a href="filesystem.constants.php#constant.lock-ex">LOCK_EX</a></code></strong> та можливість
        передавати ресурс потоку в параметр <code class="parameter">data</code>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

  
 <div class="refsect1 notes" id="refsect1-function.file-put-contents-notes">
  <h3 class="title">Примітки</h3>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: <span class="simpara">Ця функція є бінарно
безпечною.</span></p></blockquote>
  <div class="tip"><strong class="tip">Підказка</strong><p class="simpara">URL як назву файлу можна
використовувати з цією функцією, якщо ввімкнені <a href="filesystem.configuration.php#ini.allow-url-fopen" class="link">обгортки fopen</a>. Докладніше про те, як
вказати назву файлу: <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>. На сторінці <a href="wrappers.php" class="xref">Підтримувані протоколи та обгортки</a> є посилання на інформацію про можливості різних обгорток,
примітки щодо їхнього використання, інформацію про будь-які попередньо визначені
цими обгортками змінні.</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.file-put-contents-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <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.fwrite.php" class="function" rel="rdfs-seeAlso">fwrite()</a> - Binary-safe file write</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.stream-context-create.php" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - Створює контекст потока</span></li>
   </ul>
  </p>
 </div>


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