letslogic.com
letslogic.com

Reputation: 57

Best way to update a PHP based site without errors

I've got a website that I do the coding and testing for locally, and when ready I upload the files via FTP to a remote server which can be a little slow at times.

If a user tries to access a page containing a require_once of a page that's currently being uploaded, they will get errors as at that specific moment in time the file appears not to exist or is only partially uploaded. Even if a user tries to access a page directly e.g. example.php they could experience the same problem.

I overcame the require_once issue by creating a custom include function that checks the last line of the file for a marker but this doesn't help for non include pages and is really messy.

Is there a better way to update the site without pissing off the users with errors?

Upvotes: 0

Views: 125

Answers (4)

Yam Mesicka
Yam Mesicka

Reputation: 6581

1. You can always turn off all error reporting by:

error_reporting(0);

(Well, to be honest - bad solution).

2. However, that's probably not what you want. Another way of getting error silenced, is to add @ before the line. So you can solve it by writing:

@require_once(FILE)

(Again - bad solution. You don't want to just get error silenced).

3. You maybe want to use include_once instead of require_once, which will give you the ability to continue loading the page even if the file isn't there.

(Probably can do better, nah?)

4. The most recommended thing to do is to check if file_exists first, and then doing whatever you want to do. If the page is necessary for the rest of the code, you might want to put your own custom error message and stop the loading of the page.

try {
    if (!file_exists('page.php')) {
        throw new Exception('Page has not been found, please try again later.');
    }
    if (!@include_once('page.php'))
    {
        throw new Exception('Page under construction, please try again later.');
    }
}
catch(Exception $e) {    
  echo $e->getMessage();
}

(I think that's the real thing).

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212412

Use a symbolic link to point to your web application directory, and reference that symbolic link in your webserver config. Upload everything to a different directory to the live web app; then simply change the symbolic link when everything is in place.

EDIT

Alternatively.... if you're using APC for opcode caching, upload everything, and the original should still be in the opcode cache (you should have the apc stat test disabled in a production server); then flush the opcode cache when everything has been uploaded.

Upvotes: 0

deceze
deceze

Reputation: 522076

Have you looked into deployment options other than FTP? Many deployment tools like Capistrano, which works via SSH, copy new files into a new folder, then symlink the current directory to the new folder if and when the whole upload and update process is finished. This ensures atomic (all or nothing) updates.

Upvotes: 1

bumerang
bumerang

Reputation: 1846

Yes, in Your main idex file witch You should upload first put something like this:

<h1>Page under update</h1>
<h2>Please revisit after 10 minuts</h2>
<?php die();?>
// ... Your code is belowe

Now You can update other files and when it's over remove those above lines from main index.php

Upvotes: 0

Related Questions