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:<br />".$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.
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 <img> 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 '<img src="'.$binary_data.'" 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.
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://ift.tt/2LMckFM";
//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 "<b>The content of the text file :</b><br />".$content."<br /><br />";
//Close the file handler
fclose($fh);
//Set the external link of binary file
$url = "https://ift.tt/37zdHAd";
//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 "<b>The content of the binary file :</b><br />".'<img src="'.$binary_data.'" 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.
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.
from Linux Hint https://ift.tt/2Wq4IL1
0 Comments