I wrote the below code to output the contents of a file twice. But it only did it once. Why is that?
The text file contents are as follows:
My name is Sam. Sam I am.
My name is Chris and Chris I am.
The brown fox jumped over the fence.
The code is as follows:
<?php
$file = "files/info.txt";
$handle = fopen($file, "rb");
echo fread($handle, filesize($file));
echo fread($handle, filesize($file));
?>
The output:
"My name is Sam. Sam I am. My name is Chris and Chris I am. The brown fox jumped over the fence."
Invoking fread() "uses up" the file and in order to fread() it again you have to move the reading pointer back to the beginning of the file using rewind($handle)
For the first fread(), file pointer moves to end of the file, so second time nothing printed. execute the bellow code , you will come to know.
<?php
$file = "info.txt";
$handle = fopen($file, "rb");
echo fread($handle, filesize($file)-10);
print "-----";
echo fread($handle, filesize($file));
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With