fread() Function is one of the PHP operations used in filesystem and stream workflows. Reads up to a specified number of bytes from a stream. The examples below show what it does and how to handle its result.
What Is fread() Function?
Reads up to a specified number of bytes from a stream.
Its syntax is:
fread($stream, int $length)
The parameters are:
$stream— An open stream resource.int $length— Maximum bytes to read or number of bytes to process.
Return value: The declared return type is string|false. fread() may return fewer bytes than requested; loop for complete reads.
Important: fread() may return fewer bytes than requested; loop for complete reads.
Example 1: Basic Usage
The basic example focuses on the behavior of fread() Function: reads up to a specified number of bytes from a stream. Inputs are defined locally or shown as application-controlled paths.
<?php
$stream = fopen("php://memory", "w+");
fwrite($stream, "ABCDEF");
rewind($stream);
echo fread($stream, 3);
fclose($stream);
The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.
Example 2: A Different Use Case
This second example illustrates another input or execution condition for fread() Function. Notice the different checks, arguments, or output handling.
<?php
$stream = fopen(__FILE__, "rb");
while (!feof($stream)) { $block = fread($stream, 8192); if ($block === false) { break; } echo strlen($block) . " bytes\n"; }
fclose($stream);
This variation matters because fread() may return fewer bytes than requested; loop for complete reads.
Practical Example: Using the Operation in an Application
The following example demonstrates how fread() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.
<?php
$stream = fopen(__DIR__ . "/archive.bin", "rb");
if ($stream === false) { throw new RuntimeException("Cannot open archive"); }
try { $signature = fread($stream, 4); echo bin2hex($signature); } finally { fclose($stream); }
The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.
Important Behavior and Edge Cases
- Function behavior: Reads up to a specified number of bytes from a stream.
- Limitation: fread() may return fewer bytes than requested; loop for complete reads.
Comparison With Related Functions
| Function or Method | Difference |
|---|---|
fread() Function | Reads up to a specified number of bytes from a stream. |
fgets() | Reads through the next newline, while fread() requests a number of bytes. |
Use fread() Function for the specific behavior described above. The related operation, fgets(), serves a different purpose; do not substitute it unless that different behavior is intended.
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating stream or file operations as guaranteed complete on the first call. | fread() may return fewer bytes than requested; loop for complete reads. Handle false returns, partial I/O, stream position and closing resources. |
| Treating fgets() as an interchangeable replacement for fread() Function. | Reads through the next newline, while fread() requests a number of bytes. |
Key Takeaways
- Reads up to a specified number of bytes from a stream.
- fread() may return fewer bytes than requested; loop for complete reads.
- Related operation: fgets() — Reads through the next newline, while fread() requests a number of bytes.
fread() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.