Does Apache read-lock files before serving them?

后端 未结 2 605
说谎
说谎 2021-01-17 14:49

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

2条回答
  •  星月不相逢
    2021-01-17 15:44

    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.

提交回复
热议问题