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?
-
19Yes. PHP automatically closes file handles at script termination. In any sort of cicumstances where the script will not be immediately terminating following file IO, it's probably a good idea to close the file, however. e.g., if you write a library, and that library is used by a CLI script that calls your library function hundreds of times, it had better close the file after each call.– Frank FarmerCommented May 18, 2011 at 0:55
-
2@Frank, that should have been an answer so it could be upvoted. :)– sarnoldCommented May 18, 2011 at 2:40
7 Answers
Yes. But, it's good practice to do it yourself. Also, you'll leave the file open during the entire exection of the remainder of the script, which you should avoid. So unless your script finishes execution directly after you're finished writing, you're basically leaving the file open longer than you need to.
-
5Is that a bad thing to leave it open? What are the downsides to leaving it open?– locoboyCommented May 18, 2011 at 0:58
-
8You're using more memory, and other processes may not be able to use that file.– dtbarneCommented May 18, 2011 at 0:59
-
3You need to back up your claim with a citation of the php.net manual or the source code of the interpreter. Commented Jun 21, 2016 at 7:47
-
2There are also limits to how many open file handles the O/S allows, so if you're iterating through thousands of files, your PHP script will eventually (probably) crash if you're not closing the files when you're done with them.– mattCommented Aug 5, 2020 at 14:17
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:
- Retry the operation
- Unroll the changes
- Return a failure condition to a calling function
- Report the problem to the user
- Document the problem in a log file
- Return a failure indication to execution environment (such as a command line shell) which may be crucial when used in a tool chain.
or some other way that fits your situation.
This is mention in the comments section of the fclose()
manual page.
There are also security implications to leaving file descriptors open: http://cwe.mitre.org/data/definitions/403.html
Your program might execute a program with different privilege levels, and a leaked file descriptor may allow private information to cross a boundary between processes of two different trust levels:
http://osvdb.org/7559
CVE-2006-5397
CVE-2007-5159
CVE-2008-3914
The fun thing with security bugs is that it might be perfectly safe when you write the initial function, but a year or two later might become unsafe due to an innocent-looking change.
Yes, PHP normally closes the file before exiting. But you should always close it manually:
1- It's a good programming practice
2- PHP can exit unexpectedly (for example, an uncaught exception). This may leave the file with something in the queue to be written, or with a lock in it.
-
3I would expect an uncaught exception to still clean up file handles. However, I've certainly seen PHP segfault more than a few times, and there's no way PHP can do any sort of cleanup in that situation Commented May 18, 2011 at 17:05
Except when the program doesn't end or takes long, it counts towards maximum open file handles in the system. But yes, PHP allows for laziness.
Not only in PHP, in every language we should close the stream when work is done. In this way we are allowing others to use that file. If we dont close it, other programs may not use it till the program ends completely (in this case page).
When a file is opened, a lock is placed on it, preventing other processes from using it. fclose()
removes this lock.
$handle
is not an object, just a pointer. So there is no destructor telling it to unlock.
-
I'm a bit confused with your pronouns here. When you say a lock is placed on it, do you mean a lock is placed on the file? If so, what does the lock have to do with using the function? What does the lock do?– locoboyCommented May 18, 2011 at 1:02
-
the lock is placed on a file. if you are writing to the file, and then another process comes along and tries to write the file - access will be denied because two different things can't write to a file at the same time. the lock prevents the second process from being able to write. Commented May 18, 2011 at 1:06
-
so this is a reason why it doesn't matter right? As long as you have different names for your pointers, then you should have no problems writing multiple files in the same program.– locoboyCommented May 18, 2011 at 1:40
-
No, any process writing to a file gets the lock so that a different process can not write to the file. If you open the file, it gets locked, if something else tries to write to that file that second write process has to wait until the file is released / unlocked - which is achieved with fclose. The sooner your close your file and release the lock, the sooner something else can write to that file again. Commented May 18, 2011 at 3:02
-
9-1, factually incorrect. PHP does not automatically place locks on files. If you want or need locking, you must expressly request a lock during or immediately after the open. Additionally, the latest point releases of PHP now require that you expressly release the lock prior to close, while the lock would previously be released with
fclose
alone.– CharlesCommented May 18, 2011 at 4:37