PHP fread() Function
Definition and Usage
The fread() function reads files (can be safely used for binary files).
Syntax
fread(file,length)
Parameter | Description |
---|---|
file | Required. Specifies the file to read. |
length | Required. Specifies the maximum number of bytes to read. |
Description
fread() reads from the file pointer file Read at most length bytes. This function stops reading the file after reading at most length number of bytes, or when EOF is reached, or (for network streams) when a packet is available, or (after opening a user space stream) when 8192 bytes have been read, whichever comes first.
Returns the string read, or false on error.
Tips and Comments
Tip:If you just want to read the contents of a file into a string, please use file_get_contents(), it performs much better than fread().
Example
Example 1
Read 10 bytes from the file:
<?php $file = fopen("test.txt","r"); fread($file,"10"); fclose($file); ?>
Example 2
Read the entire file:
<?php $file = fopen("test.txt","r"); fread($file, filesize("test.txt")); fclose($file); ?>