PHP fwrite() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fwrite() writes a string to an open file. It is the main function for saving text or data into a file you have opened with fopen().

You pass the file handle and the text to write. The function returns how many bytes were written, or false if something went wrong. fputs() is an alias that works exactly the same way.

Syntax

Call fwrite() with the values you need. Start with this line.

PHP
$bytes = fwrite($fp, 'Hello, CodeToFun!');
CallWhat it does
fwrite($fp, 'Hello')Writes the text Hello and returns 5.
fwrite($fp, 'abcdef', 3)Writes only the first 3 bytes, which is abc.
fwrite($fp, $line . PHP_EOL)Writes a line followed by a line break.

fwrite() does not add a line break for you. Add PHP_EOL when you want each piece of text on its own line. The file mode matters too: w empties the file first, while a adds to the end.

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

Parameters

ParameterDescription
streamRequired. A file handle returned by fopen(). It must be opened with a mode that allows writing.
dataRequired. The string to write to the file.
lengthOptional. If given, writing stops after this many bytes. If null or left out, the whole string is written.

Return Value

Return ValueDescription
intThe number of bytes written.
falseReturned on failure, for example when the file is opened for reading only.

PHP Examples

Example 1 — Write Text to a File

Open a file in write mode, save a greeting, and show how many bytes were written.

PHP
<?php
$path = sys_get_temp_dir() . '/fwrite_demo.txt';

$fp = fopen($path, 'w');
$bytes = fwrite($fp, 'Hello, CodeToFun!');
fclose($fp);

echo "Wrote $bytes bytes", PHP_EOL;
echo file_get_contents($path), PHP_EOL;
unlink($path);

How It Works

  1. fopen() with mode w creates the file, or empties it if it already exists.
  2. fwrite() saves the 17 characters of the greeting and returns 17.
  3. fclose() finishes the job and releases the file.
  4. file_get_contents() reads the file back so you can see what was saved.

Example 2 — Write to a Read-Only Handle

A file opened with mode r cannot be written to. fwrite() returns false, so always check the result.

PHP
<?php
$path = sys_get_temp_dir() . '/fwrite_edge.txt';
file_put_contents($path, 'original');

$fp = fopen($path, 'r');
$result = @fwrite($fp, 'new text');

if ($result === false) {
    echo "Write failed", PHP_EOL;
}

fclose($fp);
echo file_get_contents($path), PHP_EOL;
unlink($path);

How It Works

  1. The file is created with the text original.
  2. fopen() with mode r only allows reading.
  3. fwrite() cannot write, so it returns false, and the @ sign hides the PHP notice.
  4. The strict === false check prints the message and the file keeps its old content.

Example 3 — Limit the Length and Keep Writing

Use the length argument to write only part of a string, then add more text with a second call.

PHP
<?php
$path = sys_get_temp_dir() . '/fwrite_length.txt';

$fp = fopen($path, 'w');
$n = fwrite($fp, 'abcdef', 3);
fwrite($fp, 'XYZ');
fclose($fp);

echo "Wrote $n bytes", PHP_EOL;
echo file_get_contents($path), PHP_EOL;
unlink($path);

How It Works

  1. The third argument 3 tells fwrite() to stop after the first 3 bytes, so only abc is written.
  2. The return value 3 is stored in $n.
  3. The second fwrite() continues from where the first one stopped and adds XYZ.
  4. The final file content is abcXYZ.

Frequently Asked Questions

It writes a string to an open file and returns the number of bytes written, or false on failure.
There is no difference. fputs() is an alias of fwrite(), so both do the same job. Most developers prefer fwrite().
Open the file with mode a, then call fwrite(). Mode w would erase the existing content first.
fwrite() does not add one by itself. Add PHP_EOL to the end of your text, for example fwrite($fp, 'Hello' . PHP_EOL).
file_put_contents() opens, writes and closes in one call, so it is shorter for simple jobs. Use fwrite() when you write many times to the same open file.

Next: glob()

glob() finds file and folder names that match a pattern, such as every .txt file in a fold.

glob() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful