<HTML ><HEAD ><TITLE >fwrite</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Filesystem functions" HREF="ref.filesystem.html"><LINK REL="PREVIOUS" TITLE="ftruncate" HREF="function.ftruncate.html"><LINK REL="NEXT" TITLE="glob" HREF="function.glob.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.ftruncate.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.glob.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.fwrite" ></A >fwrite</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN26243" ></A ><P > (PHP 3, PHP 4 )</P >fwrite -- Binary-safe file write</DIV ><DIV CLASS="refsect1" ><A NAME="AEN26246" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >fwrite</B > ( resource handle, string string [, int length])<BR ></BR ><P > <B CLASS="function" >fwrite()</B > writes the contents of <TT CLASS="parameter" ><I >string</I ></TT > to the file stream pointed to by <TT CLASS="parameter" ><I >handle</I ></TT >. If the <TT CLASS="parameter" ><I >length</I ></TT > argument is given, writing will stop after <TT CLASS="parameter" ><I >length</I ></TT > bytes have been written or the end of <TT CLASS="parameter" ><I >string</I ></TT > is reached, whichever comes first. </P ><P > <B CLASS="function" >fwrite()</B > returns the number of bytes written, or <TT CLASS="constant" ><B >FALSE</B ></TT > on error. </P ><P > Note that if the <TT CLASS="parameter" ><I >length</I ></TT > argument is given, then the <A HREF="ref.info.html#ini.magic-quotes-runtime" >magic_quotes_runtime</A > configuration option will be ignored and no slashes will be stripped from <TT CLASS="parameter" ><I >string</I ></TT >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in <A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A > mode parameter. </P ></BLOCKQUOTE ></DIV ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN26278" ></A ><P ><B >Example 1. A simple fwrite example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?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')) { print "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (!fwrite($handle, $somecontent)) { print "Cannot write to file ($filename)"; exit; } print "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { print "The file $filename is not writable"; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.fread.html" ><B CLASS="function" >fread()</B ></A >, <A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A >, <A HREF="function.fsockopen.html" ><B CLASS="function" >fsockopen()</B ></A >, <A HREF="function.popen.html" ><B CLASS="function" >popen()</B ></A >, and <A HREF="function.fputs.html" ><B CLASS="function" >fputs()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.ftruncate.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.glob.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >ftruncate</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.filesystem.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >glob</TD ></TR ></TABLE ></DIV ></BODY ></HTML >