Jump to content

preventing more than one user from reading and writing to a file at same time


rgrne

Recommended Posts

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. 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.