定义
fread()函数用于读取文件。
语法
PHP fread()函数具有以下语法。
fread(file,length)
参数
参数
是否必须
描述
file
需要。
要读取的打开文件
length
需要。
要读取的最大字节数
返回值
此函数返回读取的字符串,或失败时为FALSE。
实例1
/*
http://www.manongjc.com/article/1800.html
作者:码农教程
*/
$huge_file = fopen("VERY_BIG_FILE.txt", "r");
while (!feof($huge_file)) {
print fread($huge_file, 1024);
}
fclose($huge_file);
?>
feof()获取文件句柄,如果你在文件的结尾则返回true,否则返回false。
fread()适合读取文件的一小部分。例如,Zip文件以字母“PK”开头,因此,我们可以快速检查以确保给定的Zip文件是否带有此代码。
/*
http://www.manongjc.com/article/1800.html
作者:码农教程
*/
$zipfile = fopen("data.zip", "r");
if (fread($zipfile, 2) != "PK") {
print "Data.zip is not a valid Zip file!";
}
fclose($zipfile);
?>
实例2
从文件中读取10个字节:
$file = fopen("test.txt","r");
fread($file,"10");
fclose($file);
?>
实例3
读取整个文件:
$file = fopen("test.txt","r");
fread($file,filesize("test.txt"));
fclose($file);
?>