Page 1 of 1

Create static page from a dynamic one but...

Posted: Sun Dec 19, 2004 8:03 pm
by tomfra
OK,

I need to do a simple thing. There is a PHP script that creates some output dynamically. Now I want to create a static version of the same page - e.g. update.php = dynamic, update.html = static.

I used something like this:

Code: Select all

copy("http://domain/dir/update.php", "update.html");
This works. But only if this code is executed from a different file - i.e. not from update.php itself. Is there a way to make it work correctly from the same file? Technically, it does work but for some reason it seems to loop several times.

It's 3 AM local time so only some clear ideas please ;)

Thanks!

Tomas

Re: Create static page from a dynamic one but...

Posted: Sun Dec 19, 2004 8:27 pm
by Appletalk
tomfra wrote:OK,

I need to do a simple thing. There is a PHP script that creates some output dynamically. Now I want to create a static version of the same page - e.g. update.php = dynamic, update.html = static.

I used something like this:

Code: Select all

copy("http://domain/dir/update.php", "update.html");
This works. But only if this code is executed from a different file - i.e. not from update.php itself. Is there a way to make it work correctly from the same file? Technically, it does work but for some reason it seems to loop several times.

It's 3 AM local time so only some clear ideas please ;)

Thanks!

Tomas
If you include the copy function in the same file, you are making a recursive page, so once it enters and executes copy(), the file is processed again and copy is called again, and again...

To solve this, the best way is not to call copy from the update.php :roll:
A not very good solution (:wink:) :

Code: Select all

if($_GET['copy'] != 1 )
      copy("http://domain/dir/update.php?copy=1", "update.html");
So you avoid recursivity.

Posted: Mon Dec 20, 2004 1:04 am
by timvw
What it comes down to: make sure you have the page that you are about to copy is completely in memory. And then write it back to disk.

The following seems to work fine (php5 only, but could use fwrite instead of file_put_contents)

Code: Select all

<?php
$file = file_get_contents(basename($_SERVER['PHP_SELF']));
file_put_contents(basename($_SERVER['PHP_SELF']), $file . "bah<br>");
?>

Posted: Mon Dec 20, 2004 8:30 am
by tomfra
timvw,

Your code does work but since it's a dynamic php code, it has to be accessed through the http protocol to make the static output. And when you change it to that, we are back to the "loop" problem.

Tomas

Posted: Mon Dec 20, 2004 2:22 pm
by rehfeld
thats why you pass a value through the query string, ?copy=1 like shown in the other post


another method would be to use output buffering


Code: Select all

<?php
// very top of script
ob_start();

echo 'foo';
?>
<html>
more foo



<?php
// very bottom of script
$output = ob_get_contents();
// now write $output to a file
?>

Posted: Mon Dec 20, 2004 3:31 pm
by timvw
actually, i've tried it multiple times by requesting http://test/test.php and it simply worked...

instead of requesting a file through a url (to get it parsed/executed) you can also read it and [/php_man]eval[php_man] it.[/php_man]