Search code examples
phpfileversion-controlchecksumrepository

PHP: write to current file


I am currently working on a web app update system. It will work kind of like version control where the master server is the repo. The difference between my system and vc would be you cannot commit/push etc to the master server.

So the idea is:

User goes to admin > update

Checks for updates

The master repo sends back a list of all the latest version files and their checksums

The app checks this list against the files on the server. If some are out of date/dont match checksum, then request those file contents, and over write the files on the server.

My question is, what if the update file itself needs updating... how would I go about updating it? Would i be able to overwrite it even though its currently executed? Or would there be an error and the whole thing blows up? I would guess that the current file is stored in ram so it can overwrite itself but I am not sure.

Thanks


Solution

  • Here is one way you can go about it:

    • Only allow updating of the master files through a 'superadmin' page
    • Create a 'registry' where any part of the program has access to it (some only to read, and some to write)
    • When a file is being updated via superadmin it checks the registry whether a file is being read
    • When the superadmin updates any file it sets the 'registry' saying that a given file is being updated (in effect locking it)
    • When the user tries to update the system it checks the registry, if the file is set to 'locked' it won't try to download it
    • When the user is able to download it, it sets the registry saying file is being read (and turns it off once finished)