read.php gets data from the flat file
write.php places data in the flat file
I have run into a problem: if read.php is accessed while write.php is in the middle of writing to flatfile.dat then only some data is called and corrupted data is displayed.
I have found that flock() is very unreliable and it does NOT protect the integrity of my data. The documentation for flock() warns that it does not work the same on all systems.
I have a few ideas and want some constructive feedback.
- Use while() to read the data in a loop. Don’t return data until it is the same twice. So if the data keeps changing PHP will keep trying to read it until it gets the same data twice.
- In write.php, first rename flatfile.dat to flatfile.dat.tmp then write to flatfile.dat.tmp then rename flatfile.dat.tmp to flatfile.dat. In read.php, if flatfile.dat does not exist and flatfile.dat.tmp does exist then wait 10 milliseconds and try again.
- In write.php, first rename the directory that flatfile.dat is in then write to flatfile.dat in the temporary directiry. After done writing to the flatfile change the directory back to its original name. In read.php, if directory that flatfile.dat lives in does not exist then wait 10 milliseconds and try again.
A database is not an option for this project. I must find a reliable way to use flat files with using PHP 4.
I would greatly appreciate all constructive comments, criticism and ideas.