Using php to save php output as html file?

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

Is this possible and if so, how do you accomplish it?

You have 2 files:
  • template.php
  • convert.php

convert.php needs to create a third file called template.html. Then open template.php and copy its generated html to template.html, save the file, and close both.

Is this possible? If not, how else would I save the output of a php file to an html file dynamically?
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

mindfullsilence wrote:
Is this possible and if so, how do you accomplish it?

You have 2 files:
  • template.php
  • convert.php

convert.php needs to create a third file called template.html. Then open template.php and copy its generated html to template.html, save the file, and close both.

Is this possible? If not, how else would I save the output of a php file to an html file dynamically?


I might have misunderstood your question now. You could ofc take all the output (all the variables that PHP wants to display as html) and store it in a variable ($textIWantToInsert) and then just write it to a html file:

Code: [ Select ]

<?php
$fileOne = "template.html";
$fileHandle = fopen($fileOne, 'w') or die("file could not be accessed/created");
$textIWantToInsert = "<h1>this is some text in my template.html file</h1>";
fwrite($fileHandle, $textIWantToInsert);
fclose($fileHandle);
//add chmod() ??
?>
  1. <?php
  2. $fileOne = "template.html";
  3. $fileHandle = fopen($fileOne, 'w') or die("file could not be accessed/created");
  4. $textIWantToInsert = "<h1>this is some text in my template.html file</h1>";
  5. fwrite($fileHandle, $textIWantToInsert);
  6. fclose($fileHandle);
  7. //add chmod() ??
  8. ?>


Not very nice code (but I'm sure you can change it.). If you want to have the info sent from convert.php to template.php you could use $_POST to pass information between files and let template.php create the template.html file.

Else if your server is running apache you can have your .html and .htm being run as .php in specific folders by doing this to the .htaccess file(s) (and perhaps make the .html files collect info from a database instead):

Code: [ Select ]
AddType application/x-httpd-php .html .htm


or:

Code: [ Select ]
AddHandler application/x-httpd-php .html .htm


Witch one you should pick depend on your web server. To be honest I haven't tested this, only read about it. There is also the option to have a iFrame in your html file and just load some completley diffrent file. :D Or use javascript (AJAX?) to "dynamically" load content.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

That's kind of the opposite of what I want. I need the output of a php file to be copied to an html file.
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

mindfullsilence wrote:
That's kind of the opposite of what I want. I need the output of a php file to be copied to an html file.


The code I posted take text from a php file and create/copy it to a html file, your php output can be stored in varibles and passed to a fwrite() command and then you could have the php output written (copied) to a html file? Maybe I'm missing something here. =O
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

You can create a http request to template.php via convert.php and capture its output. Much like you would do with any other page.

Code: [ Select ]
file_get_contents("http://yourserver.com/template.php");


You can do the same from the command line using exec.

Code: [ Select ]
exec("/usr/bin/php /path/to/your/template.php 2>/dev/null > /path/to/your/template.html")
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

Cool. Question, if you were to use this with getting the contents of a local file, would php look for it locally, or loop back to the server?
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Using file_get_contents you need something that will run the script through php interpreter. Apache does that for you, you can do a localhost call if your only running a single website off your server, otherwise you need the domain name to map to a virtualhost.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

Running into a little issue here with file_get_contents.
The original php file "template.php" works fine. It pulls some content form an xml file via simplexml. However, when I echo it out in the convert.php file with this code:
PHP Code: [ Select ]
$newsletter_contents = file_get_contents("index.php");
echo $newsletter_contents;
 
  1. $newsletter_contents = file_get_contents("index.php");
  2. echo $newsletter_contents;
  3.  

It ends up echoing the xml parser where the dynamic content should be. Eg:

Should be:
HTML Code: [ Select ]
<p>Some random text from xml node</p>
 
  1. <p>Some random text from xml node</p>
  2.  


But is, instead:
HTML Code: [ Select ]
<p><?php echo $xml->channel->item[0]->description->div[4]->div->div->p; ?>
</p>
 
  1. <p><?php echo $xml->channel->item[0]->description->div[4]->div->div->p; ?>
  2. </p>
  3.  
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

I got it to work, needed to use the output buffer functions.

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 67 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.