The readfile()
function in PHP is a built-in function that reads the contents of a file and writes the content to the output buffer.
readfile(string $filename, bool $include_path, resource $context ): int|false
filename
: This specifies the filename we have to read.
include_path
: This is an optional parameter. We set this parameter to true
if we want to search the file in the include_path (in php.ini).
context
: This is an optional parameter that specifies the behavior of the stream.
If the readfile()
function is executed successfully, it returns the number of bytes read. Otherwise, in case of any error, it returns false
.
The following code demonstrates how to use the readfile
function.
<?phpecho readfile("test.txt");?>
In the code above, invoking the readfile
function writes the content of the file to the output buffer. This is then displayed on the console, along with the number of bytes read.
Free Resources