PHP Function Reference

PHP fpassthru() Function



The PHP fpassthru() function reads from the current position in a file until EOF, and then writes the result to the output buffer.

Call rewind() to reset the file pointer to the beginning of the file if you have already written data to the file.

To dump the contents of a file to the output buffer, without first modifying it or seeking to a particular offset, you may want to use the readfile().

Note: When using this function on a binary file on Windows systems, the file should be opened in binary mode.

Syntax

fpassthru(stream)

Parameters

stream Required. Specify the file pointer to read from. It must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).

Return Value

Returns the number of characters read from stream and passed through to the output or false on failure.

Example: fpassthru() example

Lets assume that we have a file called test.txt. This file contains following content:

This is a test file.
It contains dummy content.

In the example below, the first line from the file is read using fgets() function. Then the remaining content of the file is written to the output buffer using fpassthru() function.

<?php
//open the file in read mode
$fp = fopen("test.txt", "r") or 
         die("Unable to open file!");

//reading the first line
fgets($fp);

//reading the remaining content
//and displaying it
echo fpassthru($fp);

//close the file
fclose($fp);
?>

The output of the above code will be:

It contains dummy content.26

Note: 26 indicates the number of characters passed through to the output.


❮ PHP Filesystem Reference