PHP fread() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

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

It starts at the current position of the file pointer and moves the pointer forward. You can read the whole file in one go or pick it up piece by piece in a loop.

Syntax

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

PHP
$data = fread($handle, 1024);
CallWhat it does
fread($fp, 9)Reads the next 9 bytes of the file.
fread($fp, filesize('notes.txt'))Reads the entire file at once.
fread($fp, 4096)Reads one chunk of 4096 bytes, usually inside a loop.

For plain files, fread() returns up to the number of bytes you ask for. It returns fewer when it reaches the end of the file. For very big files, read in small chunks so you do not use too much memory.

Library declarations
PHP
fread(resource $stream, int $length): string|false

Parameters

ParameterDescription
streamRequired. An open file handle returned by fopen() in a readable mode such as r.
lengthRequired. The maximum number of bytes to read. It must be greater than 0.

Return Value

Return ValueDescription
stringReturns the bytes that were read. It can be an empty string if the pointer is at the end of the file.
falseReturns false on failure.

PHP Examples

Example 1 — Read the First Bytes

Read the first nine bytes of a file.

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

$fp = fopen($file, 'r');
$data = fread($fp, 9);
fclose($fp);

echo "First 9 bytes: $data", PHP_EOL;

unlink($file);

How It Works

  1. The file holds the text CodeToFun PHP.
  2. fopen() opens it for reading, and the pointer starts at the first byte.
  3. fread() takes the next nine bytes, which spell CodeToFun.
  4. fclose() closes the file and the text is printed.

Example 2 — A Length of Zero Is an Error

PHP 8 throws a ValueError when the length is not greater than zero. Catch it to show a message.

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

$fp = fopen($file, 'r');

try {
    fread($fp, 0);
} catch (ValueError $e) {
    echo 'Error: ', $e->getMessage(), PHP_EOL;
}

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

How It Works

  1. Reading zero bytes makes no sense, so PHP 8 rejects it.
  2. fread() throws a ValueError instead of returning false.
  3. The catch block receives the error object and prints its message.
  4. Use a positive length, or file_get_contents() when you need the whole file.

Example 3 — Read a File in Chunks

Use a loop with feof() to read the file four bytes at a time.

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

$fp = fopen($file, 'r');

while (!feof($fp)) {
    $chunk = fread($fp, 4);
    echo $chunk, PHP_EOL;
}

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

How It Works

  1. The file contains ten letters.
  2. Each loop pass reads up to four bytes and prints them.
  3. The last pass finds only two bytes left, so fread() returns IJ.
  4. feof() then becomes true and the loop stops.

Frequently Asked Questions

It reads up to a given number of bytes from an open file and returns them as a string.
Pass filesize($file) as the length. For simple cases, file_get_contents() is easier.
fread() reads a number of bytes. fgets() reads one line, stopping at the new line character.
The file pointer is probably at the end of the file, or the file is empty. Use rewind() to go back to the start.
Yes. Open the file with the rb mode so that PHP does not change any bytes, which matters on Windows.

Next: fscanf()

fscanf() reads one line from an open file and splits it into values using a format string.

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