PHP fread() Function

Usage — The PHP fread() function is used to do a binary-safe file reading. The reading will stop once the function reaches length number of bytes or end of file.

It has the following syntax:

PHP

string fread ( resource $handle , int $length )

Here is an example of using fread():

PHP

$file = fopen("poem.txt","r");

// Output — Twinkle, twinkle, little
echo fread($file, 24);

fclose($file);

Return Value — This function returns the read string or FALSE on failure. It will stop at the end of the file or when it reaches the specified length, whichever comes first.

PHP Version and Changelog — The fread() function is available in PHP 4, PHP 5, PHP 7.

Relevant Functions — Other related PHP functions that you should know about are: fwrite() which does binary-safe file write, fopen() which opens file or URL, fsockopen() which opens Internet or Unix domain socket connection, popen() which opens process file pointer, fgets() which gets line from file pointer, fgetss() which gets line from file pointer and strip HTML tags, fscanf() which parses input from a file according to a format, file() which reads entire file into an array, fpassthru() which outputs all remaining data on a file pointer, ftell() which returns the current position of the file read/write pointer, rewind() which rewinds the position of a file pointer and unpack() which unpack data from binary string.

Go Directly To — usage, parameters, working examples or additional tips.

Parameters

handle

The handle parameter is used to specify a file system pointer resource that is typically created using fopen(). In simple terms, this option specifies the open file that you want to read from. This is a required parameter.

length

The length parameter is used to specify up to length number of bytes read. This is a required parameter.

Working Examples

Here are some examples of using the fread() function:

PHP

$filename = "story.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

The following example shows how to read a file in Binary mode.

PHP

$filename = "c:\\files\\splash.gif";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);

The following example shows how to read remote files using the fread() function.

PHP

$handle = fopen("http://www.example.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);

Additional Tips

Here are some of the most upvoted tips taken from the comment section of the PHP manual:

  1. I had a fread() script that hanged forever (from php manual):

    PHP

    $fp = fsockopen("example.host.com", 80);
    
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        fwrite($fp, "Data sent by socket");
        $content = "";
    
        while (!feof($fp)) {
            // This looped forever
            $content .= fread($fp, 1024);
        }
        fclose($fp);
        echo $content;
    }
    

    The problem is that sometimes end of streaming is not marked by EOF nor a fixed mark, that’s why this looped forever. This caused me a lot of headaches. I solved it using the stream_get_meta_data function and a break statement as the following shows:

    PHP

    $fp = fsockopen("example.host.com", 80);
    
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        fwrite($fp, "Data sent by socket");
        $content = "";
    
        while (!feof($fp)) {
            $content .= fread($fp, 1024);
            $stream_meta_data = stream_get_meta_data($fp); //Added line
             if($stream_meta_data['unread_bytes'] <= 0) break; //Added line
        }
    
        fclose($fp);
        echo $content;
    }
    

    Hope this will save a lot of headaches to someone.

    Suggested by - edgarinvillegas at hotmail dot com

Further Reading

  1. You can read more about the PHP fread() function on PHP.net.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top