• PHP Video Tutorials

PHP - Function fread()



The fread() function can read from an open file, and this function can stop at the end of the file or when it can reach the specified length, whichever comes first. This function can return the read string or false on failure.

Syntax

string fread ( resource $handle , int $length )

The fread() function can read up to length bytes from the file pointer referenced by the handle.

Example-1

<?php
   $filename = "/PhpProject/sample.txt";
   $handle = fopen($filename, "r");
   echo fread($handle, "30");
   
   fclose($handle);
?>

Output

Tutorialspoint
Tutorix
Hello

Example-2

<?php
   $filename = "/PhpProject/sample.txt";
   $file = fopen($filename, "r");
   
   $contents = fread($file, filesize($filename));
   echo $contents;
   
   fclose($file);
?>

Output

Tutorialspoint
Tutorix
Hello Tutorialspoint!!!!
php_function_reference.htm
Advertisements