fread()
The fread() function is a built-in PHP function that reads a specified number of bytes from a file. This function is used to read data from files.
The fread() function reads a chosen number of bytes from an open file. Unlike helpers that swallow a whole file in one call, fread() gives you precise control over how much you read at a time — which is exactly what you need for large files, binary data, or streaming content. This page covers its syntax, return value, how to read a file in safe chunks, and the gotchas that trip people up.
Syntax
fread(resource $stream, int $length): string|false| Parameter | Description |
|---|---|
$stream | A file pointer (resource) returned by fopen(). |
$length | The maximum number of bytes to read. |
fread() stops as soon as one of these happens: $length bytes have been read, the end of the file (EOF) is reached, a packet becomes available (for network streams), or 8192 bytes have been read after the stream buffer was filled (for non-regular files). It returns the bytes read as a string, or false on failure.
fread()is binary-safe — it reads bytes exactly as they are, so it works for images, PDFs, and other non-text files, not just plain text.
Reading an entire file
The simplest use is to read a whole file in one call by passing its size as $length:
<?php
$filename = 'myfile.txt';
$file = fopen($filename, 'r');
if ($file) {
$data = fread($file, filesize($filename));
echo $data;
fclose($file);
}Here fopen() opens the file in read-only mode ('r'), filesize() reports how many bytes the file holds, and fread() reads exactly that many. Always pair fopen() with fclose() so the file handle is released.
For reading a whole file when you don't need a handle,
file_get_contents()is a one-line alternative. Reach forfread()when you need to control the read size or process the file incrementally.
Reading a file in chunks
For large files you should not load everything into memory at once. Read a fixed-size chunk per iteration and stop at end of file using feof():
<?php
$file = fopen('large.log', 'r');
if ($file) {
while (!feof($file)) {
$chunk = fread($file, 1024); // read up to 1 KB at a time
echo $chunk;
}
fclose($file);
}This keeps memory usage flat regardless of file size, because only one 1 KB chunk lives in memory at a time. Increase the chunk size (for example 8192) to trade more memory for fewer read calls.
Return value and error handling
fread() returns a string of the bytes it read. At end of file it returns an empty string "", and on failure it returns false. Because "" and false are both falsy, use a strict check when you need to tell them apart:
<?php
$file = fopen('data.bin', 'rb');
if ($file === false) {
exit('Could not open the file.');
}
$bytes = fread($file, 16);
if ($bytes === false) {
echo 'Read failed.';
} else {
echo 'Read ' . strlen($bytes) . " bytes.\n";
}
fclose($file);Note the 'rb' mode: on Windows, opening in binary mode (b) prevents line-ending translation that would corrupt binary data. It's harmless on other platforms, so it's good practice for any non-text file.
Common gotchas
$lengthis a maximum, not a guarantee. A singlefread()may return fewer bytes than requested (for example near EOF or on a network stream). To read an exact amount, loop until you've collected enough or hit EOF.- The file must be opened in a readable mode. Modes like
'w'open for writing only; use'r','r+','a+', etc. Seefopen()for the full mode list. - Don't forget
fclose(). Leaking handles can exhaust the available file descriptors in long-running scripts. - Reading lines? If you want one line at a time rather than a byte count,
fgets()is the better tool.
Related functions
fopen()— open a file and get the pointerfread()needs.fwrite()— the counterpart that writes bytes to a file.fgets()— read a single line instead of a byte count.feof()— test for end of file when looping.file-get-contents()— read an entire file in one call.fclose()— close the file handle when you're done.
Conclusion
fread() is PHP's tool for reading a precise number of bytes from an open file. Use it with filesize() to grab a whole file, or loop it with feof() to stream large files in memory-friendly chunks. Remember that $length is an upper bound, open binary files with the b flag, and always close the handle with fclose().