rgrne Posted December 13, 2008 Share Posted December 13, 2008 Suppose that when a user takes on action (clicks on something), the script opens a file for read and write, reads the contents into an array, then manipulates the array, then writes the array back into the file, then closes the file. If a second user happens to click just a little later than the first, does the fact that the file has been opened for the first user prevent it from being opened and manipulated by the second user, at least until it is closed for the first user? If not, is there an easy way to cause that to be the case? I do not want two users who accidentally clicked at the same time to cause the script to manipulate two copies of the original array at once, then have one overwrite the results of the other. I want the "later" user's manipulation to always occur after the first user's is completed, using the results of the first user's manipulation. Link to comment https://forums.phpfreaks.com/topic/136783-preventing-more-than-one-user-from-reading-and-writing-to-a-file-at-same-time/ Share on other sites More sharing options...
mrdamien Posted December 13, 2008 Share Posted December 13, 2008 Sounds like you should use a database instead, but here you go: http://ca3.php.net/manual/en/function.flock.php Link to comment https://forums.phpfreaks.com/topic/136783-preventing-more-than-one-user-from-reading-and-writing-to-a-file-at-same-time/#findComment-714389 Share on other sites More sharing options...
chronister Posted December 13, 2008 Share Posted December 13, 2008 Yeah, you should use a database to do this kind of thing. It looks like flock can have some serious issues by reading the comments on the functions page I would use a database to handle this kind of thing. Either replace the whole file aspect of it and use a database transaction and keep everything in the database, or use a database table to keep track of when a file was opened, who has "checked" the file out, and when that check-out was released. This sounds like the best way to approach this task. If you can't get rid of editing files, and move everything into the database, then set up a "check-out" system that keeps track of the file. You would have to account for someone checking the file out and not checking it back in, so you would have to determine the length of time a file would be considered abandoned based on your needs. Thats all I got to offer.. hope it helps ya. Nate Link to comment https://forums.phpfreaks.com/topic/136783-preventing-more-than-one-user-from-reading-and-writing-to-a-file-at-same-time/#findComment-714445 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.