是否可以在实时更换服务器上的PHP文件?

If I want to update a PHP file on the server using FTP, can I replace it while the website is live and it may be executed any time?

Could there be a 404 error (or any other error) if a user tries to execute the php file at the exact moment it is being replaced? Or will there be some kind of file 'lock' while it is being overwritten?

Should I put the server in 'maintenance mode' first?

There will be some or other issues if some user is accessing that file while you updating it via FTP. It is always a good practice to put your site on maintenance mode and then update the files.

This depends on how does your FTP server act. In most cases there will be a very short time, when your PHP script will be incomplete (partially uploaded), which cause a parse/syntax error.

So, if you have a high-load website with many request per second to this script, the better way is upload script with another name and then rename it.

In theory, replacing a live file could in fact cause a 404, among other issues. If this is something you're worried about, putting the server in "maintenance mode" would be the safest approach.

Additionally, you should look into a more robust deployment process than just dumping files over FTP. Version control systems like Git make this sort of thing a bit simpler, as you can work on things locally and then update everything with a single command on the server. It's also common to have a "take-the-site-offline" step of such a process to make the transition safe and smooth. The more files you want to change, the more important this becomes.

In reality, the chances of a low-traffic site throwing a 404 during an FTP transfer are slim, but it's something you need to keep in mind because there could be potential risks involved, depending on your application and your environment.

Have a look at Vincent Driessen's Git branching model. It outlines some really good practices. Especially when working with other people on a project.

Yes is possible you cnìan change the file on fly.Put the server in 'maintenance mode' first is a good pratice for a final check before a full use

My advice: everything you do to a live website should be done in a reversible manner.

  1. Test your file on a development server
  2. Put your website in maintenance mode.
  3. Make a backup of all your code and database.
  4. Update your file Check that everything is oK
  5. Put the website live once again

PHP loads files as needed, without regard for whether or not they are being replaced. So, if you have many PHP files (class files for example) and replace them all, without locking down your site, then there is a chance that both old and new files will be loaded -- or even that a file cannot be found.

There are two extensions you can use to solve this problem; one for PHP and one for Apache:

Both extensions work with symlinks (symbolic links; pointers that point to where a path can be found). When the request starts the extensions will resolve the symlink (figure out where the symlink points to) and then cache it. Requests will then only use the resolved path. Without using one of these extensions PHP will resolve the path every time a file is needed, so if you change it mid-request then PHP will look in the wrong place.

When you are done updating your files you can just change the symlink to where the new files are stored. Requests that are already running will keep using the old path, and new requests will use the new path.