Write to an open file pointer.
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().
| Name | Description | Optional |
|---|---|---|
stream |
The file pointer resource from fopen(). | No |
data |
The string to write. | No |
length |
Maximum number of bytes to write. | Yes |
$fp = fopen('log.txt', 'a');
fwrite($fp, "New log entry\n");
fclose($fp);