<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Write a string to a file</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.file-get-contents.html">file_get_contents</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.file.html">file</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.file-put-contents" class="refentry"> <div class="refnamediv"> <h1 class="refname">file_put_contents</h1> <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">file_put_contents</span> — <span class="dc-title">Write a string to a file</span></p> </div> <div class="refsect1 description" id="refsect1-function.file-put-contents-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>file_put_contents</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$data</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span> [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span> ]] )</div> <p class="para rdfs-comment"> This function is identical to calling <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>, <span class="function"><a href="function.fwrite.html" class="function">fwrite()</a></span> and <span class="function"><a href="function.fclose.html" class="function">fclose()</a></span> successively to write data to a file. </p> <p class="para"> If <em><code class="parameter">filename</code></em> does not exist, the file is created. Otherwise, the existing file is overwritten, unless the <strong><code>FILE_APPEND</code></strong> flag is set. </p> </div> <div class="refsect1 parameters" id="refsect1-function.file-put-contents-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">filename</code></em></span> <dd> <p class="para"> Path to the file where to write the data. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">data</code></em></span> <dd> <p class="para"> The data to write. Can be either a <span class="type"><a href="language.types.string.html" class="type string">string</a></span>, an <span class="type"><a href="language.types.array.html" class="type array">array</a></span> or a <span class="type"><span class="type stream">stream</span></span> resource. </p> <p class="para"> If <em><code class="parameter">data</code></em> is a <span class="type"><span class="type stream">stream</span></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.html" class="function">stream_copy_to_stream()</a></span>. </p> <p class="para"> You can also specify the <em><code class="parameter">data</code></em> parameter as a single dimension array. This is equivalent to <em>file_put_contents($filename, implode('', $array))</em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">flags</code></em></span> <dd> <p class="para"> The value of <em><code class="parameter">flags</code></em> can be any combination of the following flags, joined with the binary OR (<em>|</em>) 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>FILE_USE_INCLUDE_PATH</code></strong> </td> <td> Search for <em><code class="parameter">filename</code></em> in the include directory. See <a href="ini.core.html#ini.include-path" class="link">include_path</a> for more information. </td> </tr> <tr> <td> <strong><code>FILE_APPEND</code></strong> </td> <td> If file <em><code class="parameter">filename</code></em> already exists, append the data to the file instead of overwriting it. </td> </tr> <tr> <td> <strong><code>LOCK_EX</code></strong> </td> <td> Acquire an exclusive lock on the file while proceeding to the writing. </td> </tr> </tbody> </table> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">context</code></em></span> <dd> <p class="para"> A valid context resource created with <span class="function"><a href="function.stream-context-create.html" class="function">stream_context_create()</a></span>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.file-put-contents-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> This function returns the number of bytes that were written to the file, or <strong><code>FALSE</code></strong> on failure. </p> <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function may return Boolean <strong><code>FALSE</code></strong>, but may also return a non-Boolean value which evaluates to <strong><code>FALSE</code></strong>. Please read the section on <a href="language.types.boolean.html" class="link">Booleans</a> for more information. Use <a href="language.operators.comparison.html" class="link">the === operator</a> for testing the return value of this function.</p></div> </div> <div class="refsect1 examples" id="refsect1-function.file-put-contents-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2369"> <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"><?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">?></span> </span> </code></div> </div> </div> <div class="example" id="example-2370"> <p><strong>Example #2 Using flags</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?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">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 changelog" id="refsect1-function.file-put-contents-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.0.0</td> <td> Added context support </td> </tr> <tr> <td>5.1.0</td> <td> Added support for <strong><code>LOCK_EX</code></strong> and the ability to pass a stream resource to the <em><code class="parameter">data</code></em> parameter </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 notes" id="refsect1-function.file-put-contents-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function is binary-safe.</span></p></blockquote> <div class="tip"><strong class="tip">Tip</strong><p class="simpara">A URL can be used as a filename with this function if the <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">fopen wrappers</a> have been enabled. See <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> for more details on how to specify the filename. See the <a href="wrappers.html" class="xref">Supported Protocols and Wrappers</a> for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.</p></div> </div> <div class="refsect1 seealso" id="refsect1-function.file-put-contents-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - Opens file or URL</span></li> <li class="member"> <span class="function"><a href="function.fwrite.html" class="function" rel="rdfs-seeAlso">fwrite()</a> - Binary-safe file write</span></li> <li class="member"> <span class="function"><a href="function.file-get-contents.html" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - Reads entire file into a string</span></li> <li class="member"> <span class="function"><a href="function.stream-context-create.html" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - Creates a stream context</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.file-get-contents.html">file_get_contents</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.file.html">file</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>