Jump to content

Copy() multiple dynamic pages to static files


dk1983

Recommended Posts

Hi there,

 

I've created a database driven web site and am trying to implement a script to go through the dynamic pages and save them statically. Ive got something that almost works - basically i just loop through the pages I want to 'save' and copy the source dynamic page to a new html file.

 

This does work, the copy() command seems to read the source URL as if a user had visited it; the content is pulled from the DB; everything is fine and the static page is 'saved'.

 

The trouble is, copy() seems to choke when its in this loop. I'tll copy the first couple of pages, then just stop. I reckon its timing out; so I tried inserting a sleep(3) within the loop to give the copy process some time - this helped a little; but obviously this could make the script take forever - ie. number of pages * 3 seconds.

 

Any ideas how I can get these dynamic pages saved?

 

Dave.

Link to comment
Share on other sites

I could do that, however thats not how I want it to work.

 

Basically, the static pages are saved into a 'static' subfolder. I've got my htaccess file directing all requests to 'index.php'. 'index.php' then looks to see if the requested file exists in the static folder. If it does, fetch it. If not, fetch the page from the DB.

 

What im after is automation at the backend. I want a single button to 'rebuild all static html files'.

 

Dave.

Link to comment
Share on other sites

You could make the script actually create the static pages its self....

 

For example:

 


$id = (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) ? $_GET['id'] : 0;
if($id == 0) {
    echo 'invalid ID';
}
else {

     if(file_exists('static/' . $id . '.htm')) {
          readfile('static/' . $id . '.htm');
     }
     else {
          //make mysql connection and send the query and what not
          ob_start();
          //output some stuf.
          $contents = ob_get_contents();
          ob_end_flush();
          if($file = fopen('static/' . $id . '.htm', 'w')) {
                @fwrite($file, $contents);
                $contents = null;
                @fclose($file);
          }
     }
}

Link to comment
Share on other sites

Thanks for the suggestion; its a good idea but unfortunately I dont want to do that either.

 

The thing is, sometimes I actually want pages to be displayed dynamically every time i reload them. If they have the 'save' code inside them, they'll get saved the first time round and the static page will be displayed after that.

 

At the moment, I can simply delete a static html file to force its corresponding database page to be displayed instead. If I then do want that page to be displayed as static; I just click a button and I run the script mentioned above.

 

But copy() doesnt work when I try to loop that script - a 'Rebuild all' button.

 

Dave

Link to comment
Share on other sites

I just tried another method without the loop - i tried to copy() the entire 'folder' of dynamic pages into the static folder with one copy() statement. The script has been running for 20 minutes and it should only be 'saving' 5 pages.

 

I think its definately a timeout thing; maybe the server doesnt like having 5 page requests all at the same time, i dont know.

 

Thanks, your help is appreciated.

Dave.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.