fread

PHP 4, PHP 5, PHP 7, PHP 8
fread - Binary-safe file read
Manual
Code Examples

fread( resource$stream, int$length ): string|false

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.

Parameters

stream

A file system pointer resource that is typically created using fopen.

length

Up to length number of bytes read.

Return Values

Returns the read string or false on failure.

Notes

Note:

If you just want to get the contents of a file into a string, use file_get_contents as it has much better performance than the code above.

Note:

Note that fread reads from the current position of the file pointer. Use ftell to find the current position of the pointer and rewind to rewind the pointer position.

Related Functions

Example of fread

Show all examples for fread

PHP Version: