定义
fread()函数用于读取文件。
语法
PHP fread()函数具有以下语法。
fread(file,length)
参数
| 参数 | 是否必须 | 描述 |
|---|---|---|
file |
需要。 | 要读取的打开文件 |
length |
需要。 | 要读取的最大字节数 |
返回值
此函数返回读取的字符串,或失败时为FALSE。
实例1
<?php
/*
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文件是否带有此代码。
<?PHP
/*
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个字节:
<?php
$file = fopen(\"test.txt\",\"r\");
fread($file,\"10\");
fclose($file);
?>
实例3
读取整个文件:
<?php
$file = fopen(\"test.txt\",\"r\");
fread($file,filesize(\"test.txt\"));
fclose($file);
?> 继续阅读与本文标签相同的文章
上一篇 :
php fputs() 函数
-
5G核心网建设是采用SA独立组网还是NSA独立组网
2026-05-15栏目: 教程
-
买了iPhone手机,旧设备上数据怎样迁移,这有3种办法快速解决
2026-05-15栏目: 教程
-
昔日电商巨头轰然倒塌!烧光几十亿补贴后,欠下工资5600万
2026-05-15栏目: 教程
-
从做流量的梦到踏实做生意,小程序成为互联网逆袭工具!
2026-05-15栏目: 教程
-
Uber在巴黎上线摩托车租赁服务,加速共享两轮布局对击Lyft
2026-05-15栏目: 教程
