Creating static html files for ftp uploading?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
toro
Forum Newbie
Posts: 10
Joined: Wed Jul 03, 2002 8:03 am
Location: Iceland

Creating static html files for ftp uploading?

Post by toro »

Hello,
my challenge is this:

I'm toying with the idea of building a database and template system on my local machine, create static html files when I update and then (manually) ftp these new files to the server.

I have a free hosting with friends of mine, but their server is not running php.
(And no, I'm not considering changing my hosting - I see this as a interresting challenge :wink: )

The question: What is the simplest way to achieve this?

I've thought of doing this in the 'usual' way - but instead of echo($each_string); I'd add $each_string to a temporary array and at the end loop through the array and write to file.

I'm wondering if there is any way of telling PHP at the beginning of the script to send the output to a new file instead of displaying the output in the browser.

Is there any function that allows this?

(I'd prefer run the script from the browser rather than a command line - to allow preview and a user friendly input form).

Any ideas?
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

You can do this by using the Object buffering functions.

http://www.php.net/manual/en/ref.outcontrol.php
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

This could be the core of a page caching script:

Code: Select all

<?php

    // db queries, declare a bunch of vars etc
    .........
    .........

    // capture output in buffer
    ob_start();
    include("html/template.htm");
    
    // copy buffer to string
    $browser_output = ob_get_contents();

    // silently clear buffer
    ob_end_clean();

    // write file
    $fp = fopen($filename, "wb");
    fputs($fp, $browser_output);
    fclose($fp);

?>
You basically use a normal php script which builds a page, then put ob_start() (which captures browser output) before the first point in your script where something would be output to the browser.

I normally build pages by declaring a bunch of vars and then including an html template where they all get echo'd out, so the ob_start() line has to come before the include() in the above example.

When there's no more browser output (after the include() line in this example) the buffered string is saved in a var and then buffer is cleared.
Last edited by McGruff on Thu Aug 11, 2005 11:04 am, edited 1 time in total.
User avatar
toro
Forum Newbie
Posts: 10
Joined: Wed Jul 03, 2002 8:03 am
Location: Iceland

Thanks!

Post by toro »

Thanks a lot, this seems to be exactly what I was looking for!
Post Reply