PHP fpassthru()Functions


PHP Filesystem 参考手册Complete PHP Filesystem Reference

Definition and Usage

The fpassthru() function reads all data from the current position of an open file until the end of file (EOF), and writes the result to the output buffer.

This function returns the number of characters passed, or FALSE on failure.

Syntax

fpassthru(file)

Parameter Description
file Required. Specifies the open file or resource to be read.


Tips and Notes

Note:When using the fpassthru() function on binary files in Windows systems, keep in mind that the file must be opened in binary mode.

Tip:If you have already written data to the file, you must call rewind() to point the file pointer to the beginning of the file.

Tip:If you only want to output the contents of the file to the output buffer without modifying it, use the readfile() function instead, which saves you the fopen() call.


Example 1

<?php
$file = fopen("test.txt","r");

// Read first line
fgets($file);

// Send rest of the file to the output buffer
echo fpassthru($file);

fclose($file);
?>

The code above will output:

There are three lines in this file.
This is the last line.59

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);
?>


PHP Filesystem 参考手册Complete PHP Filesystem Reference Other Extensions