PHP readfile() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, readfile() reads a file and writes its content straight to the output. It does not store the content in a variable.

This makes it a great choice for sending files to the browser, such as downloads or images, because it uses very little memory even for large files.

Syntax

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

PHP
$bytes = readfile('report.txt');
CallWhat it does
readfile('report.txt')Prints the content of report.txt and returns the number of bytes.
readfile(__DIR__ . '/photo.jpg')Sends an image file to the browser when the right headers are set.
readfile('missing.txt')Returns false with a warning because the file does not exist.

readfile() prints the file for you, so you do not need echo. If you want the content in a variable instead, use file_get_contents(). Before sending a download, set the headers with header(), and make sure nothing else has already been printed.

Library declarations
PHP
readfile(string $filename, bool $use_include_path = false, ?resource $context = null): int|false

Parameters

ParameterDescription
filenameRequired. The path of the file to read.
use_include_pathOptional. Set to true to also search the include_path. The default is false.
contextOptional. A stream context resource. It is rarely needed.

Return Value

Return ValueDescription
int|falseReturns the number of bytes that were read and printed, or false on failure.

PHP Examples

Example 1 — Print a File

Create a small text file and send it to the output with readfile().

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

$bytes = readfile($file);

echo PHP_EOL, "Bytes sent: $bytes", PHP_EOL;

How It Works

  1. file_put_contents() saves the text into hello. txt.
  2. readfile() prints the file content immediately.
  3. The function returns the number of bytes it sent, which is 20.
  4. The echo prints a new line and the byte count.

Example 2 — A File That Does Not Exist

readfile() returns false for a missing file, so always check the result.

PHP
<?php
$bytes = @readfile(__DIR__ . '/missing.txt');

if ($bytes === false) {
    echo "File not found", PHP_EOL;
}

How It Works

  1. missing. txt does not exist on the disk.
  2. readfile() cannot open it and returns false.
  3. The @ hides the PHP warning so the script can show its own message.
  4. The strict === false check separates a failure from a file of 0 bytes.

Example 3 — Send a File as a Download

Set the right headers and then call readfile() so the browser saves the file.

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

if (!is_file($file)) {
    http_response_code(404);
    exit('File not found');
}

header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="report.txt"');
header('Content-Length: ' . filesize($file));

readfile($file);
exit;

How It Works

  1. is_file() checks that the file exists, and a 404 answer is sent if it does not.
  2. Content-Disposition with attachment tells the browser to download instead of showing the file.
  3. Content-Length lets the browser show a correct progress bar.
  4. readfile() streams the file to the browser without loading it all into memory.

Frequently Asked Questions

It reads a file and sends its content directly to the output, then returns the number of bytes it sent.
file_get_contents() returns the content as a string. readfile() prints it right away and saves memory, which is better for large files.
Send a Content-Disposition: attachment header before calling readfile(), and make sure nothing has been printed earlier.
Something was printed before or after the file, such as a blank line outside the PHP tags. Remove extra output and call exit after readfile().
Not by itself. Never use the raw file name from a request. Use an allowed list or basename() with a fixed folder.

Next: readlink()

readlink() returns the path that a symbolic link points to. It tells you where the link le.

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