Why do I need to finish by using the fclose($handle) function after writing to a file using php? Doesn\'t the program automatically do this when it ends?
There may be unwritten data sitting in the output buffer that doesn't get written until the file is closed. If an error occurs on the final write, you can't tell that the output is incomplete which may cause all sorts of problems much later.
By explicitly calling fclose() and checking its return value, you have the opportunity to:
or some other way that fits your situation.
This is mention in the comments section of the fclose() manual page.