English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP fread() Function Usage and Example

PHP Filesystem Reference Manual

The fread() function can read data from the opened file. This function can stop at the end of the file or when the specified length is reached, 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 the maximum number of bytes from the file pointer referenced by the handle.

Example1

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

Output Result

w3codebox
Tutorix
Hello

Example2

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

Output Result

w3codebox
Tutorix
Hello oldtoolbag.com!!!!

PHP Filesystem Reference Manual