PHP readfile() Function
Definition
The readfile() function reads a file and writes it to the output buffer.
Syntax
PHP readfile() Function has the following syntax.
readfile(filename,include_path,context)
Parameter
Parameter | is Required | Description |
---|---|---|
filename | Required. | File to read |
include_path | Optional. | '1' means to search for the file in the include_path in php.ini |
context | Optional. | Context of the file handle. |
Return
This function returns the number of bytes read on success, or FALSE and an error on failure.
You can hide the error output by adding an '@' in front of the function name.
Example
When passed a filename as readfile()'s only parameter, readfile() will attempt to open it, read it all into memory.
If successful, readfile() will return an integer equal to the number of bytes read from the file.
If unsuccessful, readfile() will return false.
Here is an example script:
<?PHP/*from w w w. j a va 2s.co m*/
$testfile = @readfile("test.txt");
if (!$testfile) {
print "Could not open file.\n";
}
?>
If readfile() fails to open the file, it will print an error message to the screen. You can suppress this by placing an @ symbol before the function call.
The code above generates the following result.