When the memory to the initial handle is released, your system may clean up the pointer and close it. With the following script:
$fp = fopen("example.txt", 'w');
$fp = fopen("example.html", 'w');
while (true);
I get the following output from inotifywait -m -r .
:
MODIFY example.txt
OPEN example.txt
MODIFY example.html
OPEN example.html
CLOSE_WRITE,CLOSE example.txt
This means that without an explicit fclose
on example.txt
, it is being implicitly closed anyway by the Zend Engine, in the same way as it would close an open handle at the end of script execution (i.e. by reference-counting).
However, I would still recommend you properly and explicitly close any file handles, especially given your use case - the memory implications of opening multiple file handles in a loop should be enough of a concern here. Add to that any issues with long-running scripts potentially grabbing locks on files, security considerations, it's best to clean the handle properly.
See also: Why do I need fclose
after writing to a file in PHP?