PHP fread() – reading a file

PHP fread() function is used for reading contents of a file. It read from the file stream pointed to by handle which is created by fopen() function.
Syntax of the PHP fwrite function :
fread( file handler, length )

Length is a parameter to specify how many bytes to be read. Read will stop at the specified length is reached.

PHP fread() statement is binary-safe , means both binary and character data can be read from a file.

Here is a sample PHP program for reading from a file using fread() function

<?php
// First we will create a file syam.txt and write a string into that
$file = fopen("syam.txt","w");
fwrite($file,"Here You can learn about how to write to a file");
fclose($file);
// Now we are going to read the contents of the created file and display the contents
$file_1 = fopen("syam.txt","r");
$data=fread($file_1,filesize("syam.txt"));
echo $data;
fclose($file_1);
?>

Its output will be :

Here You can learn about how to write to a file

This is only a basic introduction tutorial about reading a file using PHP. We will discuss more about in our later posts.

Exit mobile version