PHP fpassthru() function
Definition and Usage
fpassthru() function outputs all remaining data at the file pointer.
This function reads from the given file pointer to EOF and writes the result to the output buffer from the current position.
Syntax
fpassthru(file)
Parameter | Description |
---|---|
file | Required. Specifies the open file or resource to be read. |
Description
If an error occurs, fpassthru() returns false. Otherwise, fpassthru() returns from file the number of characters read and passed to the output.
the file pointer must be valid and must point to a file opened by fopen() or fsockopen() successfully opens (but has not been fclose() the file that has been closed ()。
Tips and Comments
Tip:If you have already written data to the file, you must call rewind() to move the file pointer to the beginning of the file.
Tip:If you do not modify the file or retrieve at a specific location, but just want to download the content of the file to the output buffer, you should use readfile(), thus saving the fopen() call.
Comments:When using fpassthru() with binary files on Windows systems, make sure to add b to the mode when opening the file with fopen() to open the file in binary mode. It is encouraged to use the b flag when handling binary files, even if the system does not need it, as this can make the script more portable.
Example
Example 1
<?php $file = fopen("test.txt","r"); // Read the first line fgets($file); // Send the rest of the file to the output buffer echo fpassthru($file); fclose($file); ?>
Output:
There are three lines in this file. This is the last line.59
Note:59 indicates the number of characters passed.
Example 2
Dump the index page of the www server:
<?php $file = fopen("http://www.example.com","r"); fpassthru($file); ?>