The readfile Function
The readfile function reads a file and immediately sends its contents to the output buffer.
It returns the number of bytes read or false in case of an error. The first parameter is the path to the file,
the second (optional) is the flag to search for the file in the include_path, the third (optional) is the stream context.
Syntax
readfile(
string $filename,
bool $use_include_path = false,
?resource $context = null
): int|false
Example
Let's output the contents of a text file:
<?php
$res = readfile('example.txt');
echo "read bytes: " . $res;
?>
Example
Let's try to read a non-existent file:
<?php
$res = readfile('nonexistent.txt');
if ($res === false) {
echo "Failed to read file";
}
?>
Example
Using the include_path flag:
<?php
$res = readfile('config.ini', true);
echo $res !== false ? "Read success" : "Read failed";
?>
See Also
-
the
file_get_contentsfunction,
which reads a file into a string -
the
fpassthrufunction,
which outputs the remainder of a file -
the
filefunction,
which reads a file into an array