fread a file that is changing
Provided the following example code:
<?php
$handle = fopen("/tmp/test_file/sometestfile", "r");
$contents = '';
while (!feof($handle)) {
$contents = fread($handle, 10);
开发者_运维百科 print $contents;
sleep(1);
}
fclose($handle);
?>
If sometestfile, which is a txt file in my case, changes during the read loop, why is the php program continuing to read from the old file? Say it is full of 1's and I cat sometestfile_new over it which is full of 2's. I am running this on Linux, is this inode related? If rewind() is added after each loop, the new file will be read instead, after the overwrite point in time.
from php.net
resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )
fopen()
binds a named resource, specified by filename
, to a stream.
the fopen()
as soon as it's called it "caches" the file and will output in the method you mention, example is your $contents = fread($handle, 10);
You can delete the file and it will still read that resource until it finishes the file !feof($handle)
You cannot do anything else with fopen()
, you just can't reread the source and continue to print it.
精彩评论