PHP readfile() Function
In this chapter you will learn:
- Definition for PHP readfile() Function
- Syntax for PHP readfile() Function
- Parameter for PHP readfile() Function
- Return for PHP readfile() Function
- Example - reads a file and writes it to the output buffer
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 ja va 2 s . c om
$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.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP readlink() Function
- Syntax for PHP readlink() Function
- Parameter for PHP readlink() Function
- Return for PHP readlink() Function
- Note for PHP readlink() Function
- Example - returns the target that the link points to