The readfile() function in PHP reads a file and writes its contents directly to the output buffer. It is commonly used to display file contents in a browser or serve files for download.
Syntax:
readfile(string $filename, bool $use_include_path = false, ?resource $context = null): int | falseParameters:
- $filename: Specifies the path of the file to read.
- $use_include_path: Optional Boolean parameter. Set it to true to search for the file in PHP's include_path.
- $context: Optional stream context that specifies additional options for accessing the file or URL.
Return Value:
- Returns the number of bytes read and written to the output.
- Returns false if the operation fails.
Reads a File Using readfile()
The following example reads and displays the contents of a text file named notes.txt.
notes.txt
Welcome to PHP!
This is a demo file.
Have a great day!<?php
echo "Contents of notes.txt:\n";
$bytesRead = readfile("notes.txt");
echo "\nTotal bytes read: " . $bytesRead;
?>
- readfile() reads the entire file and writes its contents directly to the output.
- The function returns the number of bytes read and written.
- The returned value can be stored in a variable for further processing.
Output:

Note: The byte count depends on the exact contents of the file, including line endings.
Whether a File Is Readable Before Using readfile()
Use is_file() and is_readable() to check whether the specified path refers to a readable file before attempting to read it.
<?php
$file = "document.txt";
if (is_file($file) && is_readable($file)) {
readfile($file);
} else {
echo "Unable to access the file.";
}
?>
- is_file() checks whether the path refers to a regular file.
- is_readable() checks whether the file is readable by the PHP process.
- readfile() outputs the file contents if the checks pass.
Note: These checks do not guarantee that the subsequent read will succeed, so handle possible read failures as well.
Output:

Displays an Image Using readfile()
The readfile() function can serve an image directly to the browser. Set the appropriate Content-Type header before outputting the image data.
<?php
$imagePath = "gfg.png";
if (is_file($imagePath) && is_readable($imagePath)) {
header("Content-Type: image/png");
readfile($imagePath);
exit;
}
http_response_code(404);
echo "Image not found.";
?>
- $imagePath specifies the image file.
- header("Content-Type: image/png") tells the browser to interpret the response as a PNG image.
- readfile() sends the image data to the output.
- exit prevents additional output from being appended to the image response.
Output:

Downloads a File Using readfile()
To prompt the browser to download a file, use the Content-Disposition: attachment header along with the appropriate content type.
<?php
$fileToDownload = "report1.csv";
if (is_file($fileToDownload) && is_readable($fileToDownload)) {
header("Content-Type: text/csv");
header(
'Content-Disposition: attachment; filename="' .
basename($fileToDownload) . '"'
);
readfile($fileToDownload);
exit;
}
http_response_code(404);
echo "Download failed: File not found.";
?>
- Content-Type: text/csv identifies the response as CSV data.
- Content-Disposition: attachment prompts the browser to download the file.
- basename() extracts the filename for the download header.
- readfile() sends the file contents to the browser.
Note: In a real application, validate and authorize access to the requested file. Do not allow untrusted user input to determine arbitrary filesystem paths.
Best Practices
- Check file paths and permissions before reading files.
- Send the appropriate HTTP headers before outputting file contents.
- Avoid printing HTML, whitespace, or debugging messages before sending headers.
- Do not use the @ operator to suppress errors as a substitute for proper error handling.
- Restrict access to sensitive files and validate file paths to prevent unauthorized access.
- Use the appropriate content type when serving images, documents, or downloadable files.