Releasing Resources
Posted on 15/3/07 by Tim Koschützki
This goes without much explanation. Whenever you are using any resources, like files, streams, etc, always make sure you release them.
Files
For files it is a best coding practice to use fclose once you are ready with your file operations. Here is a quick example:
The consequences of not using fclose on open file handles or stream handles are unforseeable and depend on the operating system. Maybe the file cannot be opened again. I am still researching of what could possibly happen, so stay tuned for updates to this article.
As for now, make it a habit though to always release your resources.
Using up your resources
It's of course also in PHP possible to use up all your resources. For example when you are comparing large database tables with multiple nested select statements and the like. You can alter the php.ini setting "memory_limit" and set it from the default 16MB to for example 64MB.
Always ensure that you set this setting high enough before you execute system critical code, especially on your live site, or else your webserver may go down, your mysql server may crash and the script can't execute properly and thus might corrupt data.
You can skip to the end and add a comment.
That is correct, however, it is just good coding practice to close files at the end of their usage. And that's what we are here about - good coding practices. :)
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.
I often release image resources because it is some powerful memory eaters.
Usually, the files will be closed after PHP script ends, so for most applications, there is not much a point of closing a file.