0

I know this is outside of the ordinary use for PHP. I use PHP to generate templates for web front-ends. I then deliver these templates to a development team. They request that we deliver flat HTML files.

Is there a way to utilize PHP to save out the html version of the file. I have screen-02.php to screen-62.php. I have to individually open these in a browser and save the html as screen-02.html, screen-03.html , etc. I also have jQuery at my disposal if that helps.

Thanks in advance for any help.

TurboSupra
  • 119
  • 1
  • 10

5 Answers5

4

I think it would be the easiest thing to write a Shell/Batch script and execute the PHP scripts from the CLI in stead of using them as a web page.

If you execute the following command:

php /some/page.php

You can generate the output that is wanted to your stdout, so if you use pipelining, you can easily do things like:

php /some/page.php >> /some/page.html

Or you can write a bash script (if you're on Linux) like this:

#!/bin/bash
for i in {1..5}
do
  php /some/screen-$i.php >> /some/screen-$i.html
done

I think that will be the easiest (and fastest) approach, no other technologies are required.

If you don't have access to the PHP CLI, you can do a similar thing, but in stead of using the PHP CLI you can use wget to download the pages.

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
  • 2
    wget might be better, because on cli you properly might run into problems with $_SERVER variables. – Oliver Mar 06 '13 at 14:51
4

using php output buffering? http://php.net/manual/en/function.ob-start.php

something like perhaps:

<?php

    ob_start();

    include_once("screen-01.php");

    $content = ob_get_clean();

    file_put_contents($fileName, $content);

?>

you could put in a loop to save all files at the same time as well, but depending on how many you should check the max execution time

Matt.C
  • 1,327
  • 7
  • 20
  • If you execute it in another script, this script's variables will be accessible. I would prefer to use cURL or wget. – Yoone Mar 06 '13 at 14:51
  • That's a fair point! probably a much better solution if this is often repeated. – Matt.C Mar 06 '13 at 14:53
0

The easiest way (in my opinion) would be to use output buffering to store and then save the PHP output. You can use this without access to command-line server tools.

Create a new PHP file like so:

<?php

// Start output buffering
ob_start();

// Loop through each of the individual files
for ( $j = 0; $j<= 62; $j++ ){

    ob_clean(); // Flush the output buffer
    $k = str_pad( $j, 2, '0' ); // Add zeros if a single-digit number
    require_once('screen-' . $k . '.php'); // Pull in your PHP file

    if ( ob_get_length() > 0 ){ // If it put output into the buffer, process
        $content = ob_get_contents(); // Pull buffer into $content
        file_put_contents( 'screen-' $k . '.html', $content ); // Place $content into the HTML file
    }
}

?>

And run it from the same server in the same path. Make sure the folder has write permissions (CHMOD) so it can create the new files. You should find it generates all your HTML files with the proper PHP output.

Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
0

Maybe something like this:

<?php
$file = $_GET['file'];

$html = file_get_contents('http://yoururl.com/'.$file);
file_put_contents('./savedPages/'.$file.'.htm', $html);

?>

Call it with http://yoururl.com/savePage.php?file=yourtarget.php

Oliver
  • 2,864
  • 1
  • 16
  • 27
0

When you use a templating engine like Smarty you can create the output and save it to a file without the need to display it in a browser.

$smarty = new Smarty;
$smarty->assign("variable", $variable);
$output = $smarty->fetch("templatefile.tpl");

file_put_contents("/path/to/filename.html", $output);

Smarty Documentation: http://www.smarty.net/docs/en/api.fetch.tpl

Another option would be to use the PHP outputbuffer

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78