Home PHP Filesystem readfile() PHP readfile() Function 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.
$bytes = readfile('report.txt'); Call What 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 readfile(string $filename, bool $use_include_path = false, ?resource $context = null): int|false Parameters Parameter Description filename Required. The path of the file to read. use_include_path Optional. Set to true to also search the include_path. The default is false. context Optional. A stream context resource. It is rarely needed.
Return Value Return Value Description int|false Returns the number of bytes that were read and printed, or false on failure.
PHP Examples 1. Print a File 2. A File That Does Not Exist 3. Send a File as a Download Example 1 — Print a File Create a small text file and send it to the output with readfile().
<?php
$file = __DIR__ . '/hello.txt';
file_put_contents($file, 'Hello from CodeToFun');
$bytes = readfile($file);
echo PHP_EOL, "Bytes sent: $bytes", PHP_EOL; Hello from CodeToFun
Bytes sent: 20 How It Works file_put_contents() saves the text into hello. txt.readfile() prints the file content immediately. The function returns the number of bytes it sent, which is 20. 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
$bytes = @readfile(__DIR__ . '/missing.txt');
if ($bytes === false) {
echo "File not found", PHP_EOL;
} How It Works missing. txt does not exist on the disk.readfile() cannot open it and returns false. The @ hides the PHP warning so the script can show its own message. The strict === false check separates a failure from a file of 0 bytes. Frequently Asked Questions What does readfile() do in PHP? It reads a file and sends its content directly to the output, then returns the number of bytes it sent.
What is the difference between readfile() and file_get_contents()? file_get_contents() returns the content as a string. readfile() prints it right away and saves memory, which is better for large files.
How do I force a file download with readfile()? Send a Content-Disposition: attachment header before calling readfile(), and make sure nothing has been printed earlier.
Why does readfile() show strange characters in my download? 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().
Is readfile() safe with user input? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion