PHP / flat file question

6 replies
Let's say I have a form on my site that a user submits some text into, and then my php script instantly validates and writes that text to a flat file. What would happen if multiple users submitted text into that form at the same time? (which would all need to be written to the same flat file).

Would that produce an error message, or slow down the script? Or would all of the user's text get successfully written to the flat file? (assuming that the script writes to the file in 'append' mode, of course).
#file #flat #php #question
  • Profile picture of the author gerulis
    All info should get inserted without a problem.
    {{ DiscussionBoard.errors[3187335].message }}
  • Profile picture of the author mikeonrails
    You have a high likelihood of getting a corrupt file. If you like the simplicity of flat files but want to worry less about data corruption, go with SQLite. It is a database within-a-file which uses "transactions" and "file-locking" to help protect against file corruption. If it receives simultaneous writes, it will only allow 1 person to write and retry the 2nd write later(for a certain number of seconds before timeout).

    If you have many writes, it's best to go with a typical relational database solution.
    {{ DiscussionBoard.errors[3187391].message }}
    • Profile picture of the author Brandon Tanner
      Thanks guys. For this particular project I would prefer the simplicity of flat files over using a DB, if I can get away with it. But I'll check out SQLite - that might be a good compromise.
      Signature

      {{ DiscussionBoard.errors[3189711].message }}
  • Profile picture of the author Ken Durham
    using flock() would work. Depending upon your traffic and the amount of data, you may see a slight lag, but I doubt it. I wrote a chat system for an ISP many years ago using flat files and seldom had a problem with it even though it was constantly in use. Corruption can be a danger and I did see this a time or two. You will want to make sure to perform good data filtering before committing to file. If possible, SQLite would be my choice.

    http://php.net/manual/en/function.flock.php
    Signature

    yes, I am....

    {{ DiscussionBoard.errors[3190977].message }}
    • Profile picture of the author ntemple
      If your transaction volume is low, you probably won't need to worry about losing data, but there is a chance.

      I've used a similiar method for logging traces to a file and it's worked. Realize that I don't care too much of it fails once in a while, the data is useful but not mission critical. It hasn't failed so far.

      To avoid conflicts, You may want to consider writing each form post to seperate file using a suitably random filename (tempnam() + random number, for example) [tempnam() itself isn't immune to race conditions].

      You can concat them later, if necessary.
      {{ DiscussionBoard.errors[3195457].message }}
      • Profile picture of the author Brandon Tanner
        Thanks everyone. Some good suggestions here. Will try a few of them and see what works best.
        Signature

        {{ DiscussionBoard.errors[3197981].message }}

Trending Topics