php

Use of fread() in PHP

Use of fread() in PHP
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.

//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.

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.

//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.

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.

//Set the filename with path
$filename = "/var/www/html/php/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 '';
//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.

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.

//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://fahmidasclassroom.com/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 :
".'';
fclose($fh);
?>

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

Video Tutorial

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.

Emulate Mouse clicks by hovering using Clickless Mouse in Windows 10
Using a mouse or keyboard in the wrong posture of excessive usage can result in a lot of health issues, including strain, carpal tunnel syndrome, and ...
Add Mouse gestures to Windows 10 using these free tools
In recent years computers and operating systems have greatly evolved. There was a time when users had to use commands to navigate through file manager...
Control & manage mouse movement between multiple monitors in Windows 10
Dual Display Mouse Manager lets you control & configure mouse movement between multiple monitors, by slowing down its movements near the border. Windo...