PHP fwrite( ) Function - GeeksforGeeks

PHP fwrite( ) Function

Last Updated : 29 Oct, 2026

The fwrite() function in PHP writes data to an open file. It can write text or binary data, depending on the content and file mode. The file must be opened using fopen() or another compatible function before calling fwrite().

Syntax:

fwrite(resource $stream, string $data, ?int $length = null): int | false

Parameters:

  • $stream: The file handle returned by fopen() or a compatible function.
  • $data: The string or binary data to write to the file.
  • $length: An optional parameter specifying the maximum number of bytes to write.

Return Value:

  • Returns the number of bytes written on success.
  • Returns false if writing fails. A successful call can also write fewer bytes than requested, so check the returned count when complete writes are required.

Writes Data to a File Using fwrite()

The following example creates a file named example.txt if it does not exist and writes text to it.

PHP
<?php
$file = "example.txt";
$handle = fopen($file, "w");

if ($handle !== false) {
    $text = "Hello, this is a sample text.\nWelcome to fwrite()!";
    $bytesWritten = fwrite($handle, $text);

    if ($bytesWritten !== false) {
        echo "Wrote $bytesWritten bytes to $file.";
    } else {
        echo "Failed to write data.";
    }

    fclose($handle);
} else {
    echo "Failed to open the file.";
}
?>
  • fopen($file, "w") opens the file for writing, creating it if necessary and truncating existing content.
  • fwrite() writes the specified string to the file.
  • The return value is checked before displaying the result.
  • fclose() closes the file after writing.

Note: The byte count depends on the exact string written.

Output:

Screenshot-2026-10-29-121946
Terminal Output


Screenshot-2026-10-29-122047
example.txt

Appends Data to an Existing File Using fwrite()

Use append mode (a) to add data at the end of a file without removing its existing content.

PHP
<?php
$file = "example.txt";
$handle = fopen($file, "a");

if ($handle !== false) {
    $newLine = "\nAdding a new line to the file.";
    $bytesWritten = fwrite($handle, $newLine);

    if ($bytesWritten !== false) {
        echo "Data appended successfully.";
    } else {
        echo "Failed to append data.";
    }

    fclose($handle);
} else {
    echo "Unable to open the file.";
}
?>
  • fopen($file, "a") opens the file in append mode and creates it if it does not exist.
  • fwrite() adds the specified text at the end of the file.
  • Existing content is preserved.
  • fclose() closes the file after the operation.

Output:

Screenshot-2026-10-29-122402
Terminal Output


Screenshot-2026-10-29-122502
example.txt

Writes Binary Data Using fwrite()

The fwrite() function can also write binary data. PHP's pack() function converts values into a binary string that can be stored in a file.

PHP
<?php
$fileHandle = fopen("data.bin", "wb");

if ($fileHandle !== false) {
    $binaryContent = pack("S*", 1234, 5678, 910, 1122);
    $bytesWritten = fwrite($fileHandle, $binaryContent);

    if ($bytesWritten !== false) {
        echo "Binary data written successfully.";
    } else {
        echo "Failed to write binary data.";
    }

    fclose($fileHandle);
} else {
    echo "Failed to open the file.";
}
?>
  • fopen("data.bin", "wb") opens the file in binary write mode, creating it if necessary and truncating existing content.
  • pack("S*", ...) converts the supplied integers into a binary string using the S format.
  • fwrite() writes the binary string to the file.
  • fclose() closes the file.

Note: The byte order and integer representation used by pack() depend on the format character and platform. Use an explicitly defined byte order when binary data must be portable across systems.

Output:

Screenshot-2026-10-29-122806
Terminal Output


Screenshot-2026-10-29-122847
data.bin

Best Practices

  • Check whether fopen() returns a valid file handle before writing.
  • Choose the correct file mode: use w to overwrite content and a to append content.
  • Check the return value of fwrite() to detect write failures.
  • Handle partial writes when the application requires all data to be written.
  • Close the file using fclose() after writing.
  • Use flock() when multiple processes may access the same file and coordinated locking is required.
  • Validate file paths and restrict access to prevent unauthorized file modifications.
Comment