Horje
PHP SplFileObject fpassthru() Function

The SplFileObject::fpassthru() is an inbuilt function in PHP that is used to output the contents of a file to the output buffer, typically the browser, without reading the entire file into memory. It allows you to efficiently stream the contents of a file in smaller chunks, which is particularly useful for working with large files.

Syntax:

public SplFileObject::fpassthru(): int

Parameters: This function does not have any parameters.

Return Value: This function returns the number of characters from the buffer.

Program 1: The following program demonstrates SplFileObject::fpassthru() function.

Note: Before running this program save this file as “data.txt”

John,Doe,25
Jane,Smith,30
Alice,Johnson,28

 

PHP

<?php
     
$file = new SplFileObject('data.txt', 'r');
$file->fpassthru();
  
?>

Output:

John,Doe,25
Jane,Smith,30
Alice,Johnson,28

Program 2: The following program demonstrates SplFileObject::fpassthru() function.

PHP

<?php
  
$filePath = 'output.txt';
  
// Check if the file exists
if (file_exists($filePath)) {
    $file = new SplFileObject($filePath, 'r');   
      
      // Output the file content to the browser
    $file->fpassthru();
} else {
       // Handle the case when the file doesn't exist
    echo "File not found.";
  
?>

Output:

hey Geeks for Geeks 

Reference: https://www.php.net/manual/en/splfileobject.fpassthru.php




Reffered: https://www.geeksforgeeks.org


PHP

Related
PHP SplFileObject fseek() Function PHP SplFileObject fseek() Function
PHP stripcslashes() Function PHP stripcslashes() Function
PHP mt_getrandmax() Function PHP mt_getrandmax() Function
PHP readline_add_history() Function PHP readline_add_history() Function
PHP mb_ereg_search_pos() Function PHP mb_ereg_search_pos() Function

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11