readfile()
readfile() — Outputs a file.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Reads a file and writes it to the output buffer.
Signature
readfile(string $filename, bool $use_include_path = false, ?resource $context = null): int|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $filename | string | The filename being read. |
| $use_include_path | bool | You can use the optional second parameter and set it to true, if you want to search for the file in the include_path, too. |
| $context | ?resource | A context stream resource. |
Return value
Returns the number of bytes read from the file on success, or false on failure
Example
A runnable snippet:
$file = 'monkey.gif'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="'.basename($file).'"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); readfile($file); exit; }
See also
fpassthru(), file(), fopen(), file_get_contents()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page readfile() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.