I have an mobile app that reads a JSON file that is stored on an Apache server. The contents of that JSON file are regenerated (using a PHP script) if something is changed
You're thinking in the wrong paradigm for *nix platforms. What you want are atomic file writes to the JSON file in your script. You do this by writing the file to a unique temporary filename in the target directory then using rename()
to move this file over the old one. The file move operation is atomic. Asynchronous processes will either open the old JSON file or the new one but not a hybrid.
There are various ways to construct a temporary filename. See the PHP documentation user comments on tempnam()
. My system generates a request unique ID, so I just use $_SERVER["UNIQUE_ID"]
as the base.