PHP | fread( ) Function

The fread() function in PHP is an inbuilt function which reads up to length bytes from the file pointer referenced by file from an open file. The fread() function stops at the end of the file or when it reaches the specified length passed as a parameter, whichever comes first. The file and the length which has to be read are sent as parameters to the fread() function and it returns the read string on success, or FALSE on failure.

Syntax:

string fread ( $file, $length )

Parameters Used:
The fread() function in PHP accepts two parameters.

  • $file: It is a mandatory parameter which specifies the file.
  • $length: It is a mandatory parameter which specifies the maximum number of bytes to be read.

Return Value:

  • It returns the read string on success, or False on failure.

Exceptions:

  • Both binary data, like images and character data, can be written with this function since fread() is binary-safe.
  • To get the contents of a file only into a string, use file_get_contents() as it has much better performance than the code above.
  • Since systems running Windows differentiate between binary and text files, the file must be opened with ‘b’ included in fopen() mode parameter.

Below programs illustrate the fread() function:

Suppose a file named gfg.txt contains the following content:

Geeksforgeeks is a portal of geeks!

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Opening a file
$myfile = fopen("gfg.txt", "r");
  
// reading 13 bytes from the file
// using fread() function
echo fread($myfile, "13");
  
// closing the file
fclose($myfile);
?>

chevron_right


Output:

Geeksforgeeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Opening a file
$myfile = fopen("gfg.txt", "r");
  
// reading the entire file using
// fread() function
echo fread($myfile, filesize("gfg.txt"));
        
// closing the file
fclose($myfile);
?>

chevron_right


Output:

Geeksforgeeks is a portal of geeks!

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Opening a file
$myfile = "logo.jpg";
  
// opening in binary read mode 
// for windows systems
$myhandle = fopen($myfile, "rb");
  
// reading an image using fread()
echo fread($myhandle, filesize($myfile));
        
// closing the file
fclose($myhandle);
?>

chevron_right


Output:

256

Reference:
http://php.net/manual/en/function.fread.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.