<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.14 $ -->
<!-- splitted from ./en/functions/filesystem.xml, last change in rev 1.2 -->
<refentry id="function.fwrite">
 <refnamediv>
  <refname>fwrite</refname>
  <refpurpose>Binary-safe file write</refpurpose>
 </refnamediv>
 <refsect1>
  <title>Description</title>
  <methodsynopsis>
   <type>int</type><methodname>fwrite</methodname>
   <methodparam><type>resource</type><parameter>handle</parameter></methodparam>
   <methodparam><type>string</type><parameter>string</parameter></methodparam>
   <methodparam choice="opt"><type>int</type><parameter>length</parameter></methodparam>
  </methodsynopsis>
  <simpara>
   <function>fwrite</function> writes the contents of
   <parameter>string</parameter> to the file stream pointed to by
   <parameter>handle</parameter>. If the <parameter>length</parameter>
   argument is given, writing will stop after
   <parameter>length</parameter> bytes have been written or the end
   of <parameter>string</parameter> is reached, whichever comes
   first.
  </simpara>
  <simpara>
   <function>fwrite</function> returns the number of bytes
   written, or &false; on error.
  </simpara>
  <simpara>
   Note that if the <parameter>length</parameter> argument is given,
   then the <link
   linkend="ini.magic-quotes-runtime">magic_quotes_runtime</link>
   configuration option will be ignored and no slashes will be
   stripped from <parameter>string</parameter>.
  </simpara>
  <note>
   <para>
    On systems which differentiate between binary and text files
    (i.e. Windows) the file must be opened with 'b' included in
    <function>fopen</function> mode parameter.
   </para>
  </note>
  <para>
   <example>
    <title>A simple <function>fwrite</function> example</title>
    <programlisting role="php">
<![CDATA[
<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?>
]]>
    </programlisting>
   </example>
  </para>
  <simpara>
   See also <function>fread</function>,
   <function>fopen</function>,
   <function>fsockopen</function>,
   <function>popen</function>, and
   <function>file_put_contents</function>.
  </simpara>
 </refsect1>
</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->