The fpassthru Function

The fpassthru function outputs all data from a file, starting from the current position of the file pointer until the end. The function accepts one parameter - a pointer to a file that must be opened for reading. After execution, the function returns the number of bytes output or false in case of an error.

Syntax

phpfpassthru(resource $handle): int|false

Example

Output the contents of the file 'data.txt':

php<?php
  $file = fopen('data.txt', 'r');
  fpassthru($file);
  fclose($file);
?>

Example

Check the number of bytes output:

php<?php
  $file = fopen('data.txt', 'r');
  $bytes = fpassthru($file);
  echo "Bytes output: $bytes";
  fclose($file);
?>

Example

Try using fpassthru after partially reading the file:

php<?php
  $file = fopen('data.txt', 'r');
  fgets($file); // read the first line
  fpassthru($file); // output the remainder of the file
  fclose($file);
?>

See Also

  • the readfile function,
    which outputs the contents of a file
  • the fread function,
    which reads from a file
  • the fgets function,
    which reads a line from a file