PHP fread()Functions


PHP Filesystem 参考手册Complete PHP Filesystem Reference Manual

Definition and Usage

The fread() function reads an open file.

The function stops running when it reaches the specified length or reads to the end of the file (EOF), whichever comes first.

The function returns the read string, or FALSE on failure.

Syntax

string fread ( resource $handle , int $length )

Parameter Description
handle File system pointer, which is typically a resource created by fopen().
length Required. Specifies the maximum number of bytes to read.


Tips and Notes

Tip:This function is binary-safe. (This means that both binary data (such as images) and character data can be written using this function.)


Example 1

Read 10 bytes from a file:

<?php
$file = fopen("test.txt","r");
$contents = fread($file,"10");
fclose($file);
?>


Example 2

Read the entire file:

<?php
$file = fopen("test.txt","r");
$contents = fread($file,filesize("test.txt"));
fclose($file);
?>


PHP Filesystem 参考手册Complete PHP Filesystem Reference Manual Other Extensions