PHP具有丰富的文件操作函数,最简单的读取文件函数为file_get_contents,
可以将整个文件全部读取到一个字符串中。
$content=file_get_contents('./test.txt');
file_get_contents也可以通过参数控制读取内容的开始点以及长度。
$content=file_get_contents('./test.txt',null,null,100,500);
PHP也提供类似于C语言操作文件的方法,使用fopen,fgets,fread等方法,
fgets可以从文件指针中读取一行,freads可以读取指定长度的字符串。
$fp=fopen('./test.txt','rb');
while(!feof($fp)){
echo fgets($fp); //读取一行
}
fclose($fp);
$fp=fopen('./test.txt','rb');
$contents=" ";
while(!feof($fp)){
$contents.=fread($fp,4096); //一次读取4096个字符
}
fclose($fp);
使用fopen打开的文件,最好使用fclose关闭文件指针,以避免文件句柄被占用。
来源:https://www.imooc.com/code/574