thiggins09 Posted July 30, 2007 Share Posted July 30, 2007 Hi, I have a file that is read, then modified, then the stream is opened to be written to and written to. When multiple users are accessing this file, sometimes it disregards the others edit and messes up the file. How can this be fixed? Link to comment https://forums.phpfreaks.com/topic/62427-lots-of-people-writing-to-one-file/ Share on other sites More sharing options...
thiggins09 Posted July 30, 2007 Author Share Posted July 30, 2007 If I opened the file for writing at the begining of the script does php only allow writing to happen one script at a time? Am I explaining this well enough? Link to comment https://forums.phpfreaks.com/topic/62427-lots-of-people-writing-to-one-file/#findComment-310684 Share on other sites More sharing options...
btherl Posted July 30, 2007 Share Posted July 30, 2007 Yes, the problem is well known. There are a few solutions. 1. http://sg.php.net/manual/en/function.flock.php 2. Notice this in the notes for fwrite(): Note: If handle was fopen()ed in append mode, fwrite()s are atomic (unless the size of string exceeds the filesystem's block size, on some platforms, and as long as the file is on a local filesystem). That is, there is no need to flock() a resource before calling fwrite(); all of the data will be written without interruption. If you're writing to an nfs mounted filesystem though, you may have trouble. To answer your second question, no, any number of scripts can open the same file unless you lock it. Link to comment https://forums.phpfreaks.com/topic/62427-lots-of-people-writing-to-one-file/#findComment-310688 Share on other sites More sharing options...
thiggins09 Posted July 30, 2007 Author Share Posted July 30, 2007 Thank you so much, so then would ther other files that want to write to it at the same time just queue up? Link to comment https://forums.phpfreaks.com/topic/62427-lots-of-people-writing-to-one-file/#findComment-310692 Share on other sites More sharing options...
btherl Posted July 30, 2007 Share Posted July 30, 2007 Yes they'll queue up. That may cause big problems if you have too many people trying to write to the file! So you should keep an eye on it at peak times to make sure the queue of waiting scripts is emptying fast enough. Link to comment https://forums.phpfreaks.com/topic/62427-lots-of-people-writing-to-one-file/#findComment-310704 Share on other sites More sharing options...
Adrianphp Posted July 30, 2007 Share Posted July 30, 2007 Hi, I have a file that is read, then modified, then the stream is opened to be written to and written to. When multiple users are accessing this file, sometimes it disregards the others edit and messes up the file. How can this be fixed? Maybe you should consider a small database like SQLite to work with. This should be okey and it could solve all your problems. Link to comment https://forums.phpfreaks.com/topic/62427-lots-of-people-writing-to-one-file/#findComment-310739 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.