0

Thanks to a cron PHP process, some temporary files are created and then renamed (with rename()) so as to replace files that users access.

Is it possible that a user requests a page while it's being replaced by renaming, and there is no file ? Is there such an "in-between time" (even very small) ?

Anonymous
  • 11,740
  • 3
  • 40
  • 50
Hibwen
  • 75
  • 4
  • Can you provide more details? What do you mean by "as to replace files that users access"? – Anonymous Aug 11 '15 at 22:29
  • @Anonymous : I have an existing file called "file.html", and when another file "tempfile.html" is renamed "file.html", it replaces the old file. And I want to know if someone can access "file.html" during the renaming and there is no file. – Hibwen Aug 11 '15 at 22:31
  • if someone called it from a server (file.html) and it is not present for a short time. Server send it from cache. You should also note that PHP doesn't cache information about non-existent files. So, if you call file_exists() on a file that doesn't exist, it will return FALSE until you create the file. If you create the file, it will return TRUE even if you then delete the file. However unlink() clears the cache automatically. – moskito-x Aug 12 '15 at 00:14

1 Answers1

0

If you were deleting the old file and moving a new one into it's place, it is certainly possible that some users will try to access a file that does not exist. If you are renaming it without deleting the old file (overwriting), you will not run into any issues if you're on Linux. You are out of luck if you're on windows though.

You would need to write your own locking procedure in the latter case, using flock() for example.

Anonymous
  • 11,740
  • 3
  • 40
  • 50
  • Thanks you very much. I didn't know the word "atomic" otherwise I would have searched it. – Hibwen Aug 11 '15 at 22:45
  • Atoms were originally given the name because they were thought to be indivisible ("atomus" in Latin means "smallest particle"). This was later disproved, but the name stuck around :) And now we use the term with computers. – Anonymous Aug 11 '15 at 22:47
  • Yes I knew the word in this sense but not in the programming sense. – Hibwen Aug 11 '15 at 22:48