PHP | fpassthru( ) Function

The fpassthru() function in PHP is an inbuilt function which is used to read data from a current position from a specified file until end of file and then write the result to the output buffer. The file which has to be read is sent as a parameter to the fpassthru() function and it returns the number of characters passed on success or FALSE on failure.

Syntax:

int fpassthru ( $file )

Parameters Used:
The fpassthru() function in PHP accepts one parameter.

  • file: It is a mandatory parameter which specifies the file.

Return Value:

  • It returns the number of characters passed on success or FALSE on failure.

Exceptions

  • The file should be opened in binary mode while using the fpassthru() function on a binary file on Windows.
  • rewind() function should be called to set the file pointer to the beginning of the file if you have already written to the file.
  • the readfile() function should be used if you want to dump the contents of a file to the output buffer without modifying it.

Below is the implementation of fpassthru() function.

Suppose a file gfg.txt contains the following content :

Geeksforgeeks
Portal for Geeks!

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// opening a file in read only mode
$myfile = fopen("gfg.txt", "rb");
  
// Reading the first line of the file
fgets($myfile);
  
// Sending the rest of the file 
// contents to the output buffer
echo fpassthru($myfile);
  
// closing the file
fclose($myfile);
?>

chevron_right


Output:

Portal for Geeks!17

Note: 17 indicates the number of characters passed.

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$myfile = fopen("http://www.geeksforgeeks.com", "rb");
  
// dumping index page of the server
fpassthru($myfile);
?>

chevron_right


Reference :
http://php.net/manual/en/function.fpassthru.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.