Reputation: 9549
This is my code:
$zplHandle = fopen($target_file,'w');
fwrite($zplHandle, $zplBlock01);
fwrite($zplHandle, $zplBlock02);
fwrite($zplHandle, $zplBlock03);
fclose($zplHandle);
When will the file be saved? Is it immediately after writing to it or after closing it?
I am asking this because I have Printfil listening to files in a folder and prints any file that is newly created. If PHP commits a save immediately after fwrite
, I may run into issues of Printfil not capturing the subsequent writes.
Thank you for the help.
Upvotes: 11
Views: 7278
Reputation: 712
Assuming your working in PHP 5.x, try file_put_contents() instead, as it wraps the open/write/close into one call. https://www.php.net/manual/en/function.file-put-contents.php
Upvotes: 1
Reputation: 8356
I made a tiny piece of code to test it and it seems that after fwrite
the new content will be detected immediately,not after fclose
.
Here's my test on Linux.
#!/usr/bin/env php
<?php
$f = fopen("file.txt","a+");
for($i=0;$i<10;$i++)
{
sleep(1);
fwrite($f,"something\n");
echo $i," write ...\n";
}
fclose($f);
echo "stop write";
?>
After running the PHP script ,I use tail -f file.txt
to detect the new content.And It shows new contents the same time as php's output tag.
Upvotes: 5