PHP Function Reference

PHP readfile() Function



The PHP readfile() function reads a file and writes it to the output buffer.

Note: A URL can be used as a filename with this function if the fopen wrappers have been enabled.

Syntax

readfile(filename, use_include_path, context)

Parameters

filename Required. Specify the file to read.
use_include_path Optional. Set this parameter to true, if you want to search for the file in the include_path too. include_path can be set in php.ini.
context Optional. Specify a context stream resource. Context is a set of options that can modify the behavior of a stream.

Return Value

Returns the number of bytes read from the file on success, or false on failure.

Exceptions

Upon failure, an E_WARNING is thrown. To hide the error message add '@' in front of the function name.

Example: Writing the file content to output buffer

Lets assume that we have a file called test.txt. This file contains following content:

This is a test file.
It contains dummy content.

In the example below, the readfile() function is used to read the content of this file and write it to the output buffer.

<?php
$filename =  "test.txt";

//writing the file content 
//to the output buffer
readfile($filename);
?>

The output of the above code will be:

This is a test file.
It contains dummy content.

❮ PHP Filesystem Reference