fread()
fread() — Binary-safe file read.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
fread() reads up to length bytes from the file pointer referenced by stream. Reading stops as soon as one of the following conditions is met: length bytes have been read EOF (end of file) is reached a packet becomes available or the socket timeout occurs (for network streams) if the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.
Signature
fread(resource $stream, int $length): string|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $stream | resource | A file system pointer resource that is typically created using fopen(). |
| $length | int | Up to length number of bytes read. |
Return value
Returns the read string or false on failure.
Example
A runnable snippet:
// get contents of a file into a string $filename = "/usr/local/something.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle);
See also
fwrite(), fopen(), fsockopen(), popen(), fgets(), fscanf(), file(), fpassthru(), fseek(), ftell(), rewind(), unpack()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page fread() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.