Home >Backend Development >PHP Tutorial >fread() function in PHP
fread() function reads data from an open file. The fread() function stops at the end of the file or when it reaches the specified length. Returns the read string on success. Returns FALSE on failure.
fread(file_pointer, length)
file_pointer − File system pointer resource created using fopen(). Required.
#length − The maximum number of bytes to read. Required.
If successful, the fread() function returns the read string. On failure, returns FALSE.
Suppose we have a file called "one.txt" that contains the following lines.
Cricket and Football are popular sports.
The following is an example that reads 7 bytes from a file.
<?php $file_pointer = fopen("one.txt", "r"); // fread() function echo fread($file_pointer, "7"); fclose($file_pointer); ?>
Cricket
Let’s look at an Another example of all bytes in the file "one.txt".
<?php $file_pointer = fopen("one.txt", "r"); // fread() function echo fread($file_pointer, filesize("one.txt")); fclose($file_pointer); ?>
Cricket and Football are popular sports.
The above is the detailed content of fread() function in PHP. For more information, please follow other related articles on the PHP Chinese website!