PHP Filesystem fread() Function
The PHP Filesystem fread() function is used to read from an open file, and this function can stop at the end of the file or when it can reach the specified length, whichever comes first. This function can return the read string or false on failure.
The fread() function can read up to length bytes from the file pointer referenced by the handle.
Syntax
Below is the syntax of the PHP Filesystem fread() function −
string fread ( resource $handle , int $length )
Parameters
Here are the required and optional parameters of the fread() function −
| Sr.No | Parameter & Description |
|---|---|
| 1 |
handle(Required) This is a pointer to the file from which you want to read. |
| 2 |
length(Required) The number of bytes to read from the file. |
Return Value
It returns the read string or FALSE on failure.
PHP Version
The fread() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.
Example
Here in the below PHP example we will use the PHP Filesystem fread() function to read the content of the file with the specified length.
<?php // Assign the file path here $filename = "/PhpProject/sample.txt"; // Open the file in reading mode $handle = fopen($filename, "r"); // echo the reading content using fread() echo fread($handle, "30"); //Close the file fclose($handle); ?>
Output
Here is the output of the above code −
Tutorialspoint Tutorix Hello
Example
This example code first opens a file called "sample.txt" then reads its entire content using the fread() and filesize() function, and then prints the contents to the screen.
<?php $filename = "/PhpProject/sample.txt"; $file = fopen($filename, "r"); $contents = fread($file, filesize($filename)); echo $contents; fclose($file); ?>
Output
Below is the output of the above PHP code −
Tutorialspoint Tutorix Hello Tutorialspoint!!!!
Example
Now suppose if your code is unable to open and read the specified file because the file is not present or you do not have permission to access it. So how you can handle this situation just like the below PHP code.
<?php
// Open the file in read mode
$file = fopen("/PhpProject/myfile.txt", "r");
// Check if the file opened successfully
if ($file) {
// Read 10 bytes from the file
$content = fread($file, 10);
// Check if fread was successful
if ($content !== false) {
// Display the content
echo "Read content: " . $content;
} else {
echo "Failed to read from the file.";
}
// Close the file
fclose($file);
} else {
echo "Failed to open the file.";
}
?>
Output
This will produce the following result −
Failed to open the file.
Example
Here is another example that shows reading a portion of a binary file with the help of fread() function. Also we have used the bin2hex() function which converts binary data into its hexadecimal form.
<?php
// Open the binary file in read mode
$file = fopen("example.bin", "rb");
// Check if the file opened successfully
if ($file) {
// Read 20 bytes from the file
$content = fread($file, 20);
// Check if fread was successful
if ($content !== false) {
// Display the content in hexadecimal format
echo "Read content: " . bin2hex($content);
} else {
echo "Failed to read from the file.";
}
// Close the file
fclose($file);
} else {
echo "Failed to open the file.";
}
?>
Output
This will generate the following result −
Read content: 48656c6c6f20576f726c6421a1b2c3d4e5f6a7b8c9da
Notes
- Verify that the file is always there and accessible.
- Before using fread(), make sure that fopen() was successful.
- In order to gracefully handle errors, handle the false return result from fread().
- The fread() can be used with binary files as well as text files because it can read raw binary data.
Summary
The fread() function is useful when you simply need to read a specific portion of a file into memory rather to the entire file.