PHP Manual: fwrite

Enter the keyword for better results


(PHP 3, PHP 4, PHP 5)

fwrite -- Binary-safe file write


int fwrite ( resource handle, string string [, int length] )

fwrite() writes the contents of string to the file stream pointed to by handle. If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, whichever comes first.

fwrite() returns the number of bytes written, or FALSE on error.

Note that if the length argument is given, then the magic_quotes_runtime configuration option will be ignored and no slashes will be stripped from string.

Note: On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen() mode parameter.

Note: If handle was fopen()ed in append mode, fwrite()s are atomic (unless the size of string exceeds the filesystem's block size, on some platforms, and as long as the file is on a local filesystem). That is, there is no need to flock() a resource before calling fwrite(); all of the data will be written without interruption.

Example 1. A simple fwrite() example

= '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')) {
"Cannot open file ($filename)";

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

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


} else {
"The file $filename is not writable";

See also fread(), fopen(), fsockopen(), popen(), and file_put_contents().