Open In App

PHP | fread( )

Last Updated : 05 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The fread() function is used to read a file or a stream of data in PHP. It reads up to a specified number of bytes from an open file. The function returns the content that has been read, which can then be processed or displayed.

Syntax:

string fread ( $file, $length )

In this syntax:

  • $file (resource): This is the file pointer resource, which is returned by functions like fopen() or fsockopen(). You must open the file using fopen() before calling fread().
  • $length (int): The maximum number of bytes to read. If the file is smaller than the specified length, the function will read the entire file.

Return Value:

  • Returns the content read from the file as a string.
  • If an error occurs or if the end of the file is reached, it returns false.

How fread() Works?

The fread() function reads a specific number of bytes from a file or stream. It doesn’t read the whole file automatically, you need to tell it how many bytes to read. If you want to read the entire file, you can either specify a large number of bytes or use filesize() to get the file’s size.

Now, let us understand with the help of the example:

php
<?php
$file = fopen("example.txt", "r");

if ($file) {
    $content = fread($file, filesize("example.txt"));
    fclose($file);
    echo $content;
} else {
    echo "Failed to open the file.";
}
?>

Output:

Screenshot-2025-04-05-132751

fread()

In this example:

  • The file example.txt is opened in read-only mode (“r”) using fopen().
  • The fread() function reads the file’s contents. The length passed to fread() is filesize(“example.txt”), which returns the size of the file in bytes. This ensures that the entire file is read.
  • The file is closed using fclose() after reading.
  • The content of the file is displayed on the screen.

Best Practices for Using fread()

  • Always Check for Errors: You should always verify that the file has been successfully opened before attempting to read it with fread(). You can also check the return value of fread() to ensure no errors occurred during reading.
  • Read in Chunks for Large Files: For large files, it’s best to read them in smaller chunks rather than trying to read the entire file at once. This reduces memory consumption and prevents your application from running out of memory.
  • Close the File: Always close the file using fclose() once you are done reading. This frees up system resources and ensures that the file is properly closed.
  • Handle File Not Found Errors: If the file doesn’t exist or cannot be opened, you should handle this error gracefully by checking if the file pointer is false before proceeding.
  • Use filesize() Carefully: When using fread(), passing filesize() to determine the length of the file is a simple approach. However, be cautious with very large files, as the filesize() function could return a large number, potentially causing memory issues in some cases.

Next Article

Similar Reads

  翻译: