Page 1 of 1

Creating static html files for ftp uploading?

Posted: Fri Apr 25, 2003 4:22 pm
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?

Posted: Sat Apr 26, 2003 11:29 am
by hedge
You can do this by using the Object buffering functions.

http://www.php.net/manual/en/ref.outcontrol.php

Posted: Sat Apr 26, 2003 1:46 pm
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.

Thanks!

Posted: Sat Apr 26, 2003 8:11 pm
by toro
Thanks a lot, this seems to be exactly what I was looking for!