hughmanwho asked:
I would like each user of my website to open up a text file, read in what is in the file, and write out something different. This should be done in order so that no two users are editing the file at the same time. Do I need to worry about multi-threading and race conditions while using php?
I would like each user of my website to open up a text file, read in what is in the file, and write out something different. This should be done in order so that no two users are editing the file at the same time. Do I need to worry about multi-threading and race conditions while using php?
Thanks!


4 Comments to 'Do I have to worry about race conditions when manipulating files in php?'
January 18, 2010
As long as you insure that the file CANNOT be opened by TWO or more concurrent users, no problem.
Edit:
I suggest you allow the file to be downloaded.
Once a download is ordered, LOCK the original file (or replace it with a non-editable one). (ie setting permissions to read only).
Once the Upload is completed, reset the lock.
January 20, 2010
This is a very bad way to use php, and getting many ext files edited this way can make a server unresponsive. hosting companies do not like this. In any case as one user edits the file, another user may finish their edit, and the next person saves it they will overwrite and lose the previous changes. This is one reason that web applications use a database. This would carry the data to display the page from and then each edit can be a different data entry, and every time someone enters a page they will see the new information.
January 21, 2010
As PHP is a relatively high-level language, I’d expect it’d use some kind of test-and-set method to prevent that problem. And even if it could occur, the chances are extremely small, and the worst case scenario is that one of the users’ comments doesn’t end up getting added.
January 23, 2010
start with flock()
i wouldn’t do it the way you want. I would use a database.
Leave a comment