1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Do I have to worry about race conditions when manipulating files in 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 Responses to “Do I have to worry about race conditions when manipulating files in php?”

  • just "JR" says:

    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.

    Report this comment

  • Colinc says:

    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.

    Report this comment

  • mooseboys says:

    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.

    Report this comment

  • DzSoundNirvana says:

    start with flock() http://us3.php.net/flock

    i wouldn’t do it the way you want. I would use a database.

    Report this comment

Leave a Reply

Spam Protection by WP-SpamFree

TigerDirect