fread()、fgets()、fgetc()、file_get_contents() 与 file() 函数用于从文件中读取内容。
2013-05-16 12:45
751 查看
fread()、fgets()、fgetc()、file_get_contents() 与 file() 函数用于从文件中读取内容。
fread()
fread() 函数用于读取文件(可安全用于二进制文件)。
语法:
string fread( int handle, int length )
fread() 从文件指针 handle 读取最多 length 个字节。当遇到下列任何一种情况时,会停止读取文件:
在读取完最多 length 个字节数时
达到文件末尾的时候(EOF)
(对于网络流)当一个包可用时
或(在打开用户空间流之后)已读取了 8192 个字节时
从文件中读取 10 个字节(包括空格):
<?php
$filename = "test.txt";
$fh = fopen($filename, "r");
echo fread($fh, "10");
fclose($fh);
?>
提示
如果只是想将一个文件的内容读入到一个字符串中,应该用性能更好的 file_get_contents() 。
fgets()
fgets() 函数用于从文件中读取 一行 数据,并将文件指针指向下一行。
提示:如果想在读取的时候去掉文件中的 HTML 标记,请使用 fgetss() 函数。
语法:
string fgets( int handle [, int length] )
fgets() 从 handle 指向的文件中读取一行并返回长度最多为 length-1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length-1 字节后停止。如果没有指定 length ,则默认为 1K ,或者说 1024 字节。
例子:
<?php
$fh = @fopen("test.txt","r") or die("打开 test.txt 文件出错!");
// if条件避免无效指针
if($fh){
while(!feof($fh)) {
echo fgets($fh), '<br />';
}
}
fclose($fh);
?>
fread()
fread() 函数用于读取文件(可安全用于二进制文件)。
语法:
string fread( int handle, int length )
fread() 从文件指针 handle 读取最多 length 个字节。当遇到下列任何一种情况时,会停止读取文件:
在读取完最多 length 个字节数时
达到文件末尾的时候(EOF)
(对于网络流)当一个包可用时
或(在打开用户空间流之后)已读取了 8192 个字节时
从文件中读取 10 个字节(包括空格):
<?php
$filename = "test.txt";
$fh = fopen($filename, "r");
echo fread($fh, "10");
fclose($fh);
?>
提示
如果只是想将一个文件的内容读入到一个字符串中,应该用性能更好的 file_get_contents() 。
fgets()
fgets() 函数用于从文件中读取 一行 数据,并将文件指针指向下一行。
提示:如果想在读取的时候去掉文件中的 HTML 标记,请使用 fgetss() 函数。
语法:
string fgets( int handle [, int length] )
fgets() 从 handle 指向的文件中读取一行并返回长度最多为 length-1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length-1 字节后停止。如果没有指定 length ,则默认为 1K ,或者说 1024 字节。
例子:
<?php
$fh = @fopen("test.txt","r") or die("打开 test.txt 文件出错!");
// if条件避免无效指针
if($fh){
while(!feof($fh)) {
echo fgets($fh), '<br />';
}
}
fclose($fh);
?>
相关文章推荐
- PHP读取文件函数fread,fgets,fgetc,file_get_contents和file函数的使用总结
- PHP 文件读取 fread、fgets、fgetc、file_get_contents 与 file 函数
- PHP 文件读取 fread、fgets、fgetc、file_get_contents 与 file 函数
- PHP 文件读取 fread、fgets、fgetc、file_get_contents 与 file 函数
- PHP 文件读取 fread、fgets、fgetc、file_get_contents 与 file 函数
- php中file文件操作函数readfile fread fgets fgetc以及不需要加fopen的file_get_contents file_put_contents file()
- php中file文件操作函数readfile fread fgets fgetc以及不需要加fopen的file_get_contents file_put_contents file()
- fread与file_get_contents读取函数文件
- php 在linux 用fopen() 函数打开,file_get_contents(),fread()函数 读取 另外一台服务器映射过来的文件 总是返回false,null的情况。
- php使用Url读取文件的内容及file_get_contents用法
- PHP文件操作,多行句子的读取,file()函数,file_get_contents()函数,file_put_contents()函数,is_file,统计网站pv (访问量),文件的复制 copy
- fread与file_get_contents读取文件的区别
- php读取本地文件常用函数(fopen与file_get_contents)
- file(),file_get_contents(),fopen()->fread()->fclose(),fgets(),fgetc()
- 文件内容操作篇clearerr fclose fdopen feof fflush fgetc fgets fileno fopen fputc fputs fread freopen fseek ftell fwrite getc getchar gets
- PHP读取文件所有内容到字符串 file_get_contents
- 文件内容操作篇clearerr fclose fdopen feof fflush fgetc fgets fileno fopen fputc fputs fread freopen fseek ftell fwrite getc getchar gets
- PHP file_get_contents() 函数读取文件内容
- php读取本地文件常用函数(fopen与file_get_contents)
- file_get_contents( ) 函数,读取".php"结尾文件