← All languages
PHP function of the day
Random

fwrite

Write to an open file pointer.

Description

The fwrite() function writes a string to a file opened with fopen(). It returns the number of bytes written, or false on failure.

The optional length parameter limits how many bytes are written. If omitted, the entire string is written. fwrite() is binary-safe and can write any type of data including binary content.

For quick file writes without manually managing file handles, consider using file_put_contents() instead. fwrite() is an alias of fputs().

Arguments

NameDescriptionOptional
stream The file pointer resource from fopen(). No
data The string to write. No
length Maximum number of bytes to write. Yes

Example

$fp = fopen('log.txt', 'a');
fwrite($fp, "New log entry\n");
fclose($fp);

Reference