Javatpoint Logo
Javatpoint Logo

fpassthru() Function in PHP

The fpassthru() function reads until it reaches the end of an open file and puts the result to the output buffer. If an error occurs, it returns FALSE; otherwise, it returns the number of characters read from the file reference.

Syntax:

Parameters

The parameter to this function is a file pointer. This file pointer depicts the actual file pointer, which will be used to perform various operations on the file that is opened using the app open or any other function the main condition that is required for successful operations on this file pointer. This file pointer should be opened and shouldn't be closed after passing this file pointer to this function will read the contents of that file, which are pointed by that particular file pointer till the end of the file is reached. After reading the file to the end of the file the results are added at the end of the file. and once the operation is performed it is suggested that we should close this file pointer with the help of close or any other similar function.

Return Type:

The return type of this function is of the Boolean type, which means it will return false or true value depending upon the success or failure of the specified operation. The return type of this function is a non-false value which depicts the total number of characters that have been read from the file, which is specified by the file pointer which is passed as a parameter to this function and on the other hand the return type of this function will be changed to false if there is some error occurred in reading the content of the file specified by the file pointer which is an input parameter to this function.

Sample Example:

Output:

Demo text!!
This is it!22  

Explanation:

In the sample code that is written above, first of all, we have opened a file named new.txt in reading mode with the help of the fopen function the returning value we get from this function is a file pointer that will be used as an input parameter to our fpassthru() function. In the next step, we have used the fgets() function to get the contents of the file and then we have passed this file pointer to our fpassthru() function and then fpassthru() function has read all the contents from the new.txt file and has appended the results to the end of the file and the next step we have done is closing the file pointer with the help of the fclose() function. In the output, we can see that all the text that is present in the file that is opened is displayed and in the end, a numeric value of 22 is also displayed which depicts the total number of characters that have been read by the fpassthru() function during the traversal of this whole file.

So in this article, we have seen how we can use the fpassthru() function in PHP to read the content of various files and then write the result back to that file at the end of that file. Apart from this, we can use fpassthru() functions in various scenarios also.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA