Skip to content
Related Articles

Related Articles

PHP | SplFileObject fread() Function
  • Last Updated : 20 Dec, 2018

The SplFileObject::fread() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to reads the given number of bytes from the file.


string SplFileObject::fread( $length )

Parameters: This function accepts single parameter $length which is used to specify the length to be read from file in bytes.

Return values: This function returns the string read from the file on success or false on failure.

Note: It make sure the file used in below program named as gfg.txt should have read permissions.

Below Programs illustrate the SplFileObject::fread() function in PHP:

Program 1:

// Create an Object 
$file = new SplFileObject("gfg.txt", "r");
// Read 5 bytes from file
$gfg = $file->fread(5);
// Print Result
echo ($gfg);



Program 2:

// PHP program to use array to check 
// multiple files 
$GFG = array(
foreach ($GFG as &$file_name) { 
    // Create new SplFile Object 
    $file = new SplFileObject($file_name, "r");
    $contents = $file->fread(13);
    echo $contents . "</br>";




Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :