php fread文件功能

Rob*_*cha 3 php file

我编写了下面的代码来输出文件的内容两次.但它只做了一次.这是为什么?

文本文件内容如下:

My name is Sam. Sam I am.
My name is Chris and Chris I am.
The brown fox jumped over the fence.
Run Code Online (Sandbox Code Playgroud)

代码如下:

<?php
$file = "files/info.txt";
$handle = fopen($file, "rb");
echo fread($handle, filesize($file));
echo fread($handle, filesize($file));
?>
Run Code Online (Sandbox Code Playgroud)

输出:

"My name is Sam. Sam I am. My name is Chris and Chris I am. The brown fox jumped over the fence."
Run Code Online (Sandbox Code Playgroud)

Dan*_*ard 6

调用fread()"用完"文件,为了再次fread()它你必须使用rewind($ handle)将读指针移回文件的开头

  • 如果你真的需要两次回显内容,可能最好将第一个fread()的返回值分配给变量并将变量回显两次.比每次阅读内容更有效. (3认同)