10

What happens when many requests are received to read & write to a file in PHP? Do the requests get queued? Or is only one accepted and the rest are discarded?

I'm planning to use a text based hit counter.

1
  • 1
    Take a look at my answer on flock usage: here. Might be helpful. Commented Jun 5, 2011 at 13:30

3 Answers 3

6

You can encounter the problem of race condition

To avoid this if you only need simple append data you can use

file_put_contents(,,FILE_APPEND|LOCK_EX);

and don't worry about your data integrity.

If you need more complex operation you can use flock (used for simple reader/writer problem)

For your PHP script counter I suggest you to do with something like this:

//> Register this impression
file_put_contents( $file, "\n", FILE_APPEND|LOCK_EX );

//> Read the total number of impression
echo count(file($file));

This way you don't have to implement a blocking mechanism and you can keep the system and your code script lighter

Addendum

To avoid to have to count the array file() you can keep the system even lighter with this:

//> Register this impression
file_put_contents( $file, '1', FILE_APPEND|LOCK_EX );

//> Read the total number of impression
echo filesize($file);

Basically to read the number of your counter you just need to read its filesize considering each impression add 1 byte to it

Sign up to request clarification or add additional context in comments.

9 Comments

yeah i know you can lock a file for r/w operation, but if then other 2 request get received to read or write are those request thrown or get queued ?
with file_append queued. Also there aren't any blocking for read request if you use this FILE_APPEND | LOCK_EX
"with file_append queued" what does it mean ? you mean only if i open the file in append mode ?
Maybe he doesn't want to append only. With a write lock, you can do any modification you want on the file, the other threads will be queued (not thrown) until the lock is released.
@sourav: if it becomes facebook maybe you will start using something like google analytics ^^
|
4

No, requests will not be queued, reader will get damaged data, writers will overwrite each other, data will be damaged.

You can try to use flock and x mode of fopen.
It's not so easy to code good locking mutex, so try to find existing variant, or try to move data from file to DB.

2 Comments

Do you have a source for this info? Seems to contradict some other answers here, so just wondering how you know this.
@OZ_, Do you mean c instead of x flag?
1

You can use flock() to get a lock on the file prior to read/write to it. If other threads are holding a lock on the file, flock() will wait until the other locks are released.

2 Comments

Correct me if I'm wrong, but flock will not guarantee you the exclusivity of the lock, when there are 3 writers at once... which can happen (and happened many times in my cases) in multithreaded environment, such as apache (in fork mode or in multithread mode, doesn't matter), FCGI, whatever... Request #1: flock(); fwrite(); .. #2 waits for the lock, fwrite ... #3 waits too for the lock, at the same time. fwrite... BAM, #2 and #3 writes at the same time... Am I right?
In my understanding, flock() works at the OS level, and should be thread-safe. Maybe someone with better OS knowledge can confirm this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.