php what happens if 2 people append at the same time? [duplicate]
Possible Duplicate:
PHP Simultaneous File Writes
Hello,
If I have a php script and a text file and 2 users append a long text string to the text file at the same time, what will happen? will it still go through? does it get buffered so both get appended on, or do they get dropped?
By using fopen
with the c
mode parameter, you can force the file to become locked until you fclose
the file. This would disallow more than one person to write to the file at the same time. Usually, the file-system will do this by default (depending on the OS configuration), however, using the mode parameter would cover your bases.
PHP: fopen
The specifics will depend on the platform and size of the data being written, but if both processes start writing at the same position in the file, the last one often wins so to speak. But, you can end up with data being interleaved as well, particularly when writing large amounts at a time.
Take a look at flock(). The operating system doesn't fully "do it for you".
Usually the OS will lock the file during writing, so other processes will have to wait for this write process to complete before they can start writing.
So it should incude a delay on one client or a PHP warning/error during fopen(). Depends on how the code works and what functions you're using.
精彩评论