PHP fpassthru() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fpassthru() reads everything left in an open file and sends it straight to the output.

It starts from the current position of the file pointer, so it can output a whole file or just the rest of it. It does not store the content in a variable, which saves memory.

Syntax

Call fpassthru() with the values you need. Start with this line.

PHP
$bytes = fpassthru($handle);
CallWhat it does
fpassthru($fp)Prints the remaining content of the file and returns how many bytes were sent.
rewind($fp); fpassthru($fp)Goes back to the start first, so the full file is printed.
fgets($fp); fpassthru($fp)Skips the first line and prints the rest.

fpassthru() does not close the file for you, so call fclose() afterwards. It is handy for sending a file to the browser, but remember to set the right headers first. For a whole file by name, readfile() is shorter.

Library declarations
PHP
fpassthru(resource $stream): int

Parameters

ParameterDescription
streamRequired. An open file handle returned by fopen(). The pointer position decides where the output starts.

Return Value

Return ValueDescription
intReturns the number of characters sent to the output. In PHP 8 it returns 0 if there was nothing left to send.

PHP Examples

Example 1 — Print a Whole File

Open a file and send all of its content to the output.

PHP
<?php
$file = __DIR__ . '/pass_demo.txt';
file_put_contents($file, 'Hello, CodeToFun!');

$fp = fopen($file, 'r');
$count = fpassthru($fp);
fclose($fp);

echo PHP_EOL, "Bytes sent: $count", PHP_EOL;

unlink($file);

How It Works

  1. The file holds one short line of 17 characters.
  2. fpassthru() prints the file straight to the output without using a variable.
  3. It returns the number of bytes it sent, which is saved in $count.
  4. fclose() closes the handle, and echo prints the byte count on a new line.

Example 2 — Print Only the Rest of the File

fpassthru() starts where the file pointer is. Read one line first and it prints only what is left.

PHP
<?php
$file = __DIR__ . '/lines.txt';
file_put_contents($file, "Line 1\nLine 2\nLine 3\n");

$fp = fopen($file, 'r');

echo 'First: ', fgets($fp);
echo "Rest:", PHP_EOL;
$count = fpassthru($fp);
fclose($fp);

echo "Bytes sent: $count", PHP_EOL;

unlink($file);

How It Works

  1. fgets() reads the first line, which moves the file pointer forward.
  2. fpassthru() continues from that place and prints Line 2 and Line 3.
  3. The remaining text has 14 bytes, which is what the function returns.
  4. Use rewind() before fpassthru() if you want the whole file again.

Example 3 — Send a File with Headers

Set the headers first, then stream the file. This is how many download scripts work.

PHP
<?php
$file = __DIR__ . '/report.txt';
file_put_contents($file, "Report for November\nTotal: 42");

header('Content-Type: text/plain');
header('Content-Length: ' . filesize($file));

$fp = fopen($file, 'rb');
fpassthru($fp);
fclose($fp);

unlink($file);

How It Works

  1. header() tells the browser the file is plain text and how big it is.
  2. filesize() supplies the exact length for the Content-Length header.
  3. fpassthru() streams the file to the visitor without loading it all into memory.
  4. In the command line the headers have no effect, so you only see the text.

Frequently Asked Questions

It outputs all the remaining data from an open file handle, starting at the current file pointer.
readfile() takes a file name and does everything for you. fpassthru() takes an open handle, so you can seek first or choose where to start.
No. You must call fclose() yourself after it finishes.
Yes. It streams the data without loading it all into a variable, so it uses little memory. Still, turn off output buffering for very large downloads.
The pointer may already be at the end of the file. Call rewind() first to go back to the start.

Next: fputcsv()

fputcsv() writes one row of data to an open file in CSV format. CSV means comma separated .

fputcsv() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful