jnroche Posted December 22, 2006 Share Posted December 22, 2006 hi guys!is there a way to create a file using PHP dynamically?? all i know is copy() function where you can copy an 'existing' file to another directory/filename...thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted January 2, 2007 Share Posted January 2, 2007 You can use fopen to open a file or create a new file, and fwrite to write whatever you want to it. Then use fclose to close it. Check the reference page for fwrite to see an example of the whole process:http://www.php.net/manual/en/function.fwrite.php Link to comment Share on other sites More sharing options...
reportingsjr Posted January 2, 2007 Share Posted January 2, 2007 Ive a question for you Steve, do you really need to use fclose? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 3, 2007 Share Posted January 3, 2007 It depends if you care about your data or not. Generally, it's always a good idea to close a file when you're done with it. It's very easy for something to go wrong and corrupt a file that hasn't been closed properly. If you're concerned about efficiency, the overhead is negligible.In addition to that, not closing files can also cause memory leaks (if file resources in memory are continually allocated without being freed), and if the file is locked while you have it opened and another application is trying to use it, the application will be waiting until the file gets closed and unlocked.So, yeah, it's always a good idea to close your files as soon as you can. If I'm working with file data and need to use fopen instead of file_get_contents, I use fopen, fread the entire thing, fclose, and then work on the data after I've closed the file. It's always a good practice to try and do things this way, if I looked at code that did not use fclose I would consider it a bug and fix it. Link to comment Share on other sites More sharing options...
jnroche Posted January 3, 2007 Author Share Posted January 3, 2007 It depends if you care about your data or not.In addition to that, not closing files can also cause memory leaks (if file resources in memory are continually allocated without being freed), and if the file is locked while you have it opened and another application is trying to use it, the application will be waiting until the file gets closed and unlocked.So, yeah, it's always a good idea to close your files as soon as you can. If I'm working with file data and need to use fopen instead of file_get_contents, I use fopen, fread the entire thing, fclose, and then work on the data after I've closed the file. It's always a good practice to try and do things this way, if I looked at code that did not use fclose I would consider it a bug and fix it.thanks heaps guys! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now