cgwebprojects
cgwebprojects

Reputation: 3472

multiple fopen with same file and fwrite same file at same time PHP?

I have a script that I have refreshing every 1 second in 3 different tabs in my browser the script opens a file, reads it and then writes back to it.

Is there a way to keep data consistency with this issue or does php handle it iself?

What happens if tab a opens it, edits it, and when writing tab b opens and pulls stale data?

Upvotes: 0

Views: 3244

Answers (2)

IvenMS
IvenMS

Reputation: 535

You can open the file simultaneously with different instance.

When you write the file, both will do the operation and the operation done according to its timely order.

If you are logging data into file, just put the information of current instance with the log data.

If you not need to do any operation if the file already opened, make a flag to check that. OR use flock option provided by PHP. The flag can be stored either in database or as a file. Remove the flag when operation completes. Before starting the operation, check the flag status.

Upvotes: 0

Eugen Rieck
Eugen Rieck

Reputation: 65274

Use flock to handle the concurrency

Upvotes: 1

Related Questions