Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Files and I/O

PHP fread() and fwrite() functions

Reference for PHP fread() and fwrite() (alias fputs): read and write bytes on a file handle, length arguments, partial writes, positions and common mistakes.

fread() reads up to a given number of bytes from an open handle, and fwrite() (alias fputs()) writes a string to one. Both work on the handle's current position and move it forward, so successive calls continue where the last one stopped.

They are the low-level pair behind most file code. For text you will often prefer fgets() (one line) or the all-in-one file_get_contents(), but fread() and fwrite() are what you want for binary data, chunked copying and sockets.

PHP
$h = fopen("notes.txt", "r+");
fwrite($h, "Hello");
rewind($h);
echo fread($h, 5);

Try it

PHP Writing, repositioning and reading

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Reading in chunks and bytes versus characters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
fread(resource $stream, int $length): string|false
fwrite(resource $stream, string $data, ?int $length = null): int|false
fputs(resource $stream, string $data, ?int $length = null): int|false
Name Description
$stream An open handle from fopen() or a similar function.
$length (fread) Maximum number of bytes to read. Must be greater than 0.
$data The string to write.
$length (fwrite) If given, writing stops after this many bytes.

Return value: fread() returns the bytes read, an empty string at end of file, or false on error. fwrite() returns the number of bytes written, or false on error.

Errors: fread() throws a ValueError if $length is less than 1. Writing to a handle opened read-only fails with a notice and returns false.

Usage notes

  • Bytes, not characters. fwrite($h, "cafรฉ") writes 5 bytes. UTF-8 text can be split in the middle of a character if you read in fixed chunks; decode only after joining chunks, or use line reading.
  • fread() may return less than asked on plain files only at the end of the file, but on pipes and network sockets it returns whatever has arrived. Loop until feof() when reading a stream.
  • feof() turns true after a read hits the end, not before. In a loop, check for false from fgets() or an empty fread() result, as well as feof().
  • fwrite() on sockets can write only part of the data. Compare the returned count with strlen() and write the rest.
  • Position is shared. In r+, w+ and a+ modes a write moves the same position that reading uses. Use rewind(), fseek() and ftell(). In append mode every write goes to the end regardless of position.
  • Buffering. Output may sit in a buffer until fflush() or fclose(). Another process reading the file at the same time may see a partial result.
  • Whole-file helpers. file_get_contents() reads everything, and file_put_contents() writes (and can append with FILE_APPEND and lock with LOCK_EX).
  • Large files: copy with stream_copy_to_stream() or a chunk loop, never file_get_contents() into memory.
  • fputs() is an alias with no differences; most code uses fwrite().