Create static page from a dynamic one but...

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
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Create static page from a dynamic one but...

Post 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
Appletalk
Forum Newbie
Posts: 6
Joined: Sun Dec 19, 2004 7:23 pm
Location: Argentina
Contact:

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

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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>");
?>
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post 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
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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]
Post Reply