2

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?

Community
  • 1
  • 1
David19801
  • 11,214
  • 25
  • 84
  • 127
  • Possible duplicate of **PHP Simultaneous File Writes**. The answer there should cover this question too: http://stackoverflow.com/questions/1209688/php-simultaneous-file-writes – Surreal Dreams Jan 20 '11 at 22:18

3 Answers3

0

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

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
0

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".

goat
  • 31,486
  • 7
  • 73
  • 96
-1

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.

Daniel Stelter
  • 466
  • 3
  • 6