Summary of using php fread function
- 2021-12-11 17:28:04
- OfStack
The php fread function is used to read files (which can be safely used for binary files), and its syntax is fread (file, length). The parameter file is required, which means to read open files, and length is required, which means to read the maximum number of bytes.
How to use the php fread function?
Definition and usage
The fread () function reads the file (safe for binary files).
Grammar
fread(file,length)
Parameter
file Required. Specifies that open files should be read.
length Required. Specifies the maximum number of bytes to read.
Description
fread () reads up to length bytes from the file pointer file. This function stops reading files when the maximum number of bytes of length has been read, when EOF has been reached, when 1 packet is available (for network flow), or when 8192 bytes have been read (after opening user space flow), depending on which situation is encountered first.
Returns the read string, or false if an error occurs.
Tips and Notes
Tip: If you just want to read the contents of 1 file into 1 string, use file_get_contents (), which performs much better than fread ().
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);
?>