PHP fputs() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fputs() writes a string to an open file. It is an alias of fwrite(), so both functions do exactly the same thing.

You give it a file handle from fopen() and the text to save. It returns the number of bytes written, or false if something went wrong.

Syntax

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

PHP
$bytes = fputs($handle, 'Hello');
CallWhat it does
fputs($fp, 'Hello')Writes Hello to the file and returns 5.
fputs($fp, 'CodeToFun', 4)Writes only the first 4 characters, which is Code.
fputs($fp, 'Line' . PHP_EOL)Writes a line and adds a line break at the end.

fputs() is an alias of fwrite(). The PHP manual recommends fwrite() as the main name, so pick one and use it throughout your project. The file must be opened in a mode that allows writing, such as w, a, x or c.

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

Parameters

ParameterDescription
streamRequired. An open file handle returned by fopen() in a writable mode.
dataRequired. The text to write to the file.
lengthOptional. If given, writing stops after this many bytes. The default is null, which writes everything.

Return Value

Return ValueDescription
intReturns the number of bytes written.
falseReturns false on failure, for example when the handle is read only.

PHP Examples

Example 1 — Write Text to a File

Open a file, write a message with fputs(), and show how many bytes were written.

PHP
<?php
$file = __DIR__ . '/fputs_demo.txt';

$fp = fopen($file, 'w');
$bytes = fputs($fp, 'Hello');
fclose($fp);

echo "Wrote $bytes bytes", PHP_EOL;
echo 'File contains: ', file_get_contents($file), PHP_EOL;

unlink($file);

How It Works

  1. fopen() in w mode opens the file for writing.
  2. fputs() writes the word Hello and returns 5, the number of bytes.
  3. fclose() saves and closes the file.
  4. file_get_contents() reads the file back to prove the text was saved.

Example 2 — Writing to a Read-Only Handle

A handle opened with r cannot be written to, so fputs() returns false. Check the result.

PHP
<?php
$file = __DIR__ . '/readonly.txt';
file_put_contents($file, 'original');

$fp = fopen($file, 'r');
$bytes = @fputs($fp, 'new text');

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

fclose($fp);
unlink($file);

How It Works

  1. The file is opened in r mode, which only allows reading.
  2. fputs() tries to write and fails, so it returns false.
  3. The @ sign hides the notice, and the strict check catches the failure.
  4. The original content of the file stays unchanged.

Example 3 — Limit the Number of Bytes

Pass a length to write only the first part of a string.

PHP
<?php
$file = __DIR__ . '/limit.txt';

$fp = fopen($file, 'w');
$bytes = fputs($fp, 'CodeToFun', 4);
fclose($fp);

echo "Wrote $bytes bytes", PHP_EOL;
echo 'File contains: ', file_get_contents($file), PHP_EOL;

unlink($file);

How It Works

  1. The string CodeToFun has 9 characters.
  2. The third argument 4 tells fputs() to stop after four bytes.
  3. Only Code is written, and the function returns 4.
  4. The rest of the text is simply ignored.

Frequently Asked Questions

It writes a string to an open file handle and returns the number of bytes written.
Yes. fputs() is an alias of fwrite(), so they behave in the same way. fwrite() is the preferred name in the PHP manual.
No. Add PHP_EOL to your text yourself if you want each write to be on its own line.
The handle may be closed or opened in read-only mode, the disk may be full, or the file may not be writable.
Open the file with fopen() in the a mode, then call fputs(). The old content stays and the new text is added at the end.

Next: fread()

fread() reads a set number of bytes from an open file.

fread() 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