PHP fwrite() Function

The fwrite() writes to an open file.The function will stop at the end of the file or when it reaches the specified length, whichever comes first.This function returns the number of bytes written, or FALSE on failure.

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

fwrite() writes the contents of string to the file stream pointed to by handle.

Example -

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.

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.

Note

If writing twice to the file pointer, then the data will be appended to the end of the file content : -

Example -

Example -

ParameterDescription
fileRequired. Specifies the open file to write to
stringRequired. Specifies the string to write to the open file
lengthOptional. Specifies the maximum number of bytes to write