php - concurrent file writing corrupts data?

Bob R.

New member
Hi, I'm making a website where people can vote on polls, and it records the number of people who vote for a particular choice. To do this, I read the current values from a text file, make the necessary additions, and then write this data over the original (using fopen). But if I refresh rapidly at the results page (ie trying to simulate two people voting at the same time), the data becomes corrupted. Is there any way to fix this? I was thinking of appending the answer choices rather than rewriting the file. This way, the files would be counted later using a separate program.
I am using flock to lock the file during writing and reading, but it still gets corrupted...

is data protected from corruption if I use append?
 
Back
Top