PHP has many built-in functions to read the content of any file. Some of them are fread(), readfile(), fgets(), fscanf(), file(), etc. Some functions need to open the file before reading, and some can read the file content without opening it. fread() function reads the content of the file after opening. This function works with another built-in function named fopen(). How the content of the file can be read in PHP by using the fread() function is shown in this tutorial.

Syntax:

string fread (resource $handle, int $length)

It can take two arguments and returns the particular content of a file as a string. The first argument takes the file handler of any open file, and the second argument takes the length of bytes as the number that will read. The argument value can be more than the original file size.

Create a text file

Create a comma-separated text file named employees.txt with the following content. fread() function is then used to read this text file in different ways in the next part of this tutorial.

employees.txt


E-10023, Jafar Iqbal, Manager, Sales, 08-12-2000


E-10047, Anisul Hoque, Assistant Manager, HR, 06-11-2010


E-10039, Humayan Ahmed, Accountant, Marketing, 01-06-2009


E-10027, Tamim Iqbal, Manager, HR, 02-11-2011


E-10093, Rokeya Rahman, Accountant, Sales, 05-10-2011

Example 1: Read the particular content of a text file

The following example shows how the specific content of a file can be read using the fread() function. fopen() function is used in the script to open employees.txt for reading. Next, the fread() function is used to read the first 30 bytes of the text file that will be printed later.

<?php

//Set the filename with path

$filename = “employees.txt”;

//Open the file in read mode

$fh = fopen($filename, “r”);

//Read 30 bytes from the file

$content = fread($fh, 30);

//Print the return value from fread() function

echo “The first 30 bytes of the file:
.$content;

//Close the file

fclose($fh);

?>

Output:


The following output will appear after running the script from the server. The output shows the first 30 bytes of the employees.txt file.

<img alt="http://linuxhint.com/" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/use-of-fread-in-php_1.jpg" data-lazy- height="201" src="data:image/svg xml,” width=”1282″>

Example 2: Read the full content of a text file

The following example shows how the full content of a text file can be read using the fread() function. Like the previous example, the fopen() function is used to open the employees.txt file for reading. filesize() function is used in the script to find out the total size of the opening text file. fread() function reads the full content of the file when the total file size value is passed as the second argument of this function.

<?php

//Set the filename with path

$filename = “employees.txt”;

//Open the file in read mode

$fh = fopen($filename, “r”);

//Read the full content of the file

$full_content = fread($fh, filesize($filename));

//Print the file content

echo $full_content.

;

//Close the file

fclose($fh);

?>

Output:


The following output will appear after running the script from the server. The output shows the full content of the employees.txt file.

<img alt="http://linuxhint.com/" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/use-of-fread-in-php_2.jpg" data-lazy- height="252" src="data:image/svg xml,” width=”1279″>

Example 3: Read the content of a binary file

The following example shows how an image file can be read using the fread() function. Here, the fopen() function is used to open a binary file named flower.jpeg for reading. fread() function is used with the filesize() function to read the full content of the binary file. base64_encode() function is used in the script to convert the content of the binary file into a human-readable format. Then, the tag is used to print the image.

<?php

//Set the filename with path

$filename = “https://kirelos.com/wp-content/uploads/2020/12/echo/flower.jpeg”;

//Open a binary file with read mode

$fh = fopen($filename, “rb”);

//Read the content of the file

$content = fread($fh, filesize($filename));

//Encode the content using base64_encode() method

$encoded_data = base64_encode($content);

//Set the mime type

$mime_type =‘image/gif’;

//Set the binary string to generate the image

$binary_data = ‘data:’ . $mime_type . ‘;base64,’ . $encoded_data ;

//Print the image

echo ‘<img src="http://linuxhint.com/".$binary_data.“http://linuxhint.com/” height=”200px” width=”250px”>’;

//Close the file

fclose($fh);

?>

Output:


If the flower.jpeg file exists in the current location, then the image will be displayed as output like the following.

<img alt="http://linuxhint.com/" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/use-of-fread-in-php_3.jpg" data-lazy- height="422" src="data:image/svg xml,” width=”1282″>

Example 4: Read the file from an external link

The local existing text and binary files are used in the previous three examples. The following example shows how the file from the external link can be read by using the fread() function. An external text file is opened for reading by using the fopen() function. The first fread() function is used to read 1024 bytes from the external text file. Then an external link of a binary file is opened for reading using the fopen() function. The second fread() function is used to read 10000 bytes from the external binary file. The binary content will convert into the human-readable format like the previous example and print the image.

<?php

//Set the external link of text

$url = “https://fahmidasclassroom.com/sample.txt”;

//Declare file handler for reading the text file

$fh = fopen($url, “r”);

//Read the 1024 bytes of the file

$content = fread($fh, 1024);

//Print the defined bytes of the file

echo The content of the text file :
.$content.

;

//Close the file handler

fclose($fh);

//Set the external link of binary file

$url = “https://kirelos.com/wp-content/uploads/2020/12/echo/photo.jpg”;

//Declare file handler for reading the binary file

$fh = fopen($url, “rb”);

//Read the 10000 bytes of the file

$content = fread($fh, 10000);

//Encode the content using base64_encode() method

$encoded_data = base64_encode($content);

//Set the mime type

$mime_type =‘image/gif’;

//Set the binary string to generate the image

$binary_data = ‘data:’ . $mime_type . ‘;base64,’ . $encoded_data ;

//Print the image

echo The content of the binary file :
.‘<img src="http://linuxhint.com/".$binary_data.“http://linuxhint.com/” height=”200px” width=”250px”>’;

fclose($fh);

?>

Output:


If the photo.jpeg file exists in the provided location, then the image will be displayed as output like the following.

<img alt="http://linuxhint.com/" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/use-of-fread-in-php_4.jpg" data-lazy- height="598" src="data:image/svg xml,” width=”1284″>

Conclusion

The uses of the fread() function to read the text and binary files are explained in this tutorial using various examples. This function can be used for reading both the internal and external files. Hopefully, the readers will be able to use the fread() function in the PHP script for reading files after practicing the examples of this tutorial.

About the author

<img alt="Fahmida Yesmin" data-lazy-src="https://kirelos.com/wp-content/uploads/2020/12/echo/channel-logo-150×150.jpg5fde13a091b06.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.