fwrite() Function is one of the PHP operations used in filesystem and stream workflows. Writes bytes to a stream and returns the number of bytes written. The examples below show what it does and how to handle its result.
What Is fwrite() Function?
Writes bytes to a stream and returns the number of bytes written.
Its syntax is:
fwrite($stream, string $data, ?int $length = null)
The parameters are:
$stream— An open stream resource.string $data— The bytes or value being written.?int $length = null— Maximum bytes to read or number of bytes to process.
Return value: The declared return type is int|false. Partial writes are possible; repeat until all bytes are written.
Important: Partial writes are possible; repeat until all bytes are written.
Example 1: Basic Usage
The basic example focuses on the behavior of fwrite() Function: writes bytes to a stream and returns the number of bytes written. Inputs are defined locally or shown as application-controlled paths.
<?php
$stream = fopen("php://temp", "w+");
$bytesWritten = fwrite($stream, "Hello");
echo $bytesWritten;
fclose($stream);
The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.
Example 2: A Different Use Case
This second example illustrates another input or execution condition for fwrite() Function. Notice the different checks, arguments, or output handling.
<?php
$stream = fopen("php://temp", "w+");
$payload = "Hello world";
$written = 0;
while ($written < strlen($payload)) {
$bytes = fwrite($stream, substr($payload, $written));
if ($bytes === false || $bytes === 0) { throw new RuntimeException("Write failed"); }
$written += $bytes;
}
fclose($stream);
This variation matters because partial writes are possible; repeat until all bytes are written.
Practical Example: Using the Operation in an Application
The following example demonstrates how fwrite() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.
<?php
$output = fopen("php://temp", "w+");
$record = "id,name\n1,Alex\n";
$written = fwrite($output, $record);
if ($written !== strlen($record)) { throw new RuntimeException("Incomplete write"); }
rewind($output);
echo stream_get_contents($output);
fclose($output);
The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.
Important Behavior and Edge Cases
- Function behavior: Writes bytes to a stream and returns the number of bytes written.
- Limitation: Partial writes are possible; repeat until all bytes are written.
Comparison With Related Functions
| Function or Method | Difference |
|---|---|
fwrite() Function | Writes bytes to a stream and returns the number of bytes written. |
file_put_contents() | Writes the supplied data with file open and close handled internally. |
Use fwrite() Function for the specific behavior described above. The related operation, file_put_contents(), serves a different purpose; do not substitute it unless that different behavior is intended.
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating stream or file operations as guaranteed complete on the first call. | Partial writes are possible; repeat until all bytes are written. Handle false returns, partial I/O, stream position and closing resources. |
| Treating file_put_contents() as an interchangeable replacement for fwrite() Function. | Writes the supplied data with file open and close handled internally. |
Key Takeaways
- Writes bytes to a stream and returns the number of bytes written.
- Partial writes are possible; repeat until all bytes are written.
- Related operation: file_put_contents() — Writes the supplied data with file open and close handled internally.
fwrite() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.