W3docs

fputs()

The fputs() function is a built-in PHP function that writes a string to a file. This function is used to write data to files.

What is the fputs() Function?

The fputs() function writes a string to an open file. It is the workhorse for saving text to disk in PHP: logs, exports, generated config files, and so on.

The most important fact about fputs() is that it is an alias of fwrite() — the two functions are identical in every way. PHP keeps fputs() around because the name reads as "file put string," which is familiar to programmers coming from C. New code usually prefers fwrite(), but both behave the same, so anything below applies to either name.

Syntax

fputs(resource $stream, string $data, ?int $length = null): int|false
  • $stream — a file pointer (a resource) returned by fopen(). This is the open file you are writing into, not a filename.
  • $data — the string to write.
  • $length — optional. If given, writing stops after $length bytes, even if $data is longer.

It returns the number of bytes written, or false on failure. Note it returns 0, not false, when there is nothing to write, so always compare with === when checking for errors.

How to Use the fputs() Function

Writing to a file is always a three-step pattern:

  1. Open the file with fopen(), choosing a mode (see below).
  2. Call fputs() as many times as you need.
  3. Close the file with fclose() to flush the buffer and release the handle.
<?php

$file = fopen('myfile.txt', 'w');   // open for writing, truncating the file
fputs($file, "Hello, world!\n");    // write a line
fclose($file);                      // flush + release the handle

After this runs, myfile.txt contains Hello, world! followed by a newline. The \n is inside double quotes so PHP turns it into a real line break (a literal \n in single quotes would be written verbatim).

Choosing the Right fopen() Mode

The mode you pass to fopen() decides where fputs() writes and whether existing content survives. These are the modes that allow writing:

ModeStarts atTruncates file?Creates if missing?
'w'beginningyes (erases all)yes
'a'endnoyes
'x'beginningn/a (fails if file exists)yes
'r+'beginningnono (must exist)

Use 'w' to overwrite, and 'a' (append) to add to a log without losing what is already there.

<?php

// Append three lines to a log; each run adds to the end.
$log = fopen('app.log', 'a');
fputs($log, "2026-06-21 user logged in\n");
fputs($log, "2026-06-21 report generated\n");
fclose($log);

Limiting How Much You Write

The optional third argument caps the number of bytes written. This is handy when you only want a prefix of a larger string:

<?php

$file = fopen('clip.txt', 'w');
$written = fputs($file, 'Hello, world!', 5);  // write only the first 5 bytes
fclose($file);

echo $written;   // 5  — the file now contains "Hello"

$written is 5, and clip.txt holds just Hello.

Always Check the Return Value

A write can fail — a full disk, a read-only file, or a closed handle. Because fputs() can legitimately return 0, test the result with the strict === false comparison:

<?php

$file = fopen('out.txt', 'w');

if (fputs($file, 'data') === false) {
    echo 'Write failed!';
} else {
    echo 'Write succeeded.';
}

fclose($file);

This prints Write succeeded. and avoids the classic bug where if (!fputs(...)) wrongly treats a valid 0-byte write as an error.

fputs() vs. file_put_contents()

fputs() needs an open handle and is ideal when you write incrementally (many small writes, like a log loop). When you simply want to dump a whole string to a file in one shot, file_put_contents() does the open/write/close for you in a single call:

<?php

// Equivalent one-liner — no fopen()/fclose() needed.
file_put_contents('myfile.txt', "Hello, world!\n");

Reach for fputs()/fwrite() when you need control over the handle (appending across many calls, partial writes); reach for file_put_contents() for one-and-done writes.

Summary

  • fputs() is an alias of fwrite() — same behavior, two names.
  • It writes to an open file pointer from fopen(), so always pair it with fclose().
  • It returns the byte count, or false on error — check with === false.
  • Pick 'w' to overwrite and 'a' to append; an optional length limits the bytes written.
  • For a single full-file write, prefer file_put_contents(). To read the data back, see fgets() and fread().

Practice

Practice
What is the correct usage of the fputs() function in PHP?
What is the correct usage of the fputs() function in PHP?
Was this page helpful?