isedeasy Posted October 8, 2009 Share Posted October 8, 2009 Hi Everyone, I have a page that pulls the content from a MySQL db, it works fine how it is but I want my site to be more efficient. When I add new content (via a form in the admin area) I want the site to generate a static HTML copy of the PHP page. This way the page only needs to call the db when I update the site rather than everytime somebody loads the page. I found a few scripts that claim to do this but none of them seem to parse the php before creating the HTML page. Has anyone any pointers on how to go about this? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/176984-solved-convert-dynamic-php-page-to-static-html/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 8, 2009 Share Posted October 8, 2009 You would need to include the file using the URL to the .php page, then use ob_get_contents() to capture the resulting output - http://www.php.net/manual/en/function.ob-get-contents.php Then simply write the captured content to the file of your choice. Quote Link to comment https://forums.phpfreaks.com/topic/176984-solved-convert-dynamic-php-page-to-static-html/#findComment-933143 Share on other sites More sharing options...
isedeasy Posted October 8, 2009 Author Share Posted October 8, 2009 Thanks for the quick reply, I will take a look at that later and let everyone know how I get on. I better pop into the HTML section to see if I can answer somebody's question, I can't just take without giving back Quote Link to comment https://forums.phpfreaks.com/topic/176984-solved-convert-dynamic-php-page-to-static-html/#findComment-933147 Share on other sites More sharing options...
twittoris Posted October 8, 2009 Share Posted October 8, 2009 I actually just did this. Put this on top for where you want to start capturing output: ob_start(); put this on the bottom of the php $HtmlCode= ob_get_contents(); ob_end_flush(); $fh=fopen('NewPage.html','w'); fwrite($fh,$HtmlCode); fclose($fh); Quote Link to comment https://forums.phpfreaks.com/topic/176984-solved-convert-dynamic-php-page-to-static-html/#findComment-933152 Share on other sites More sharing options...
isedeasy Posted October 10, 2009 Author Share Posted October 10, 2009 Cheers for the code twittoris, I want to make a function so that I can use it for multiple pages. I came up with the following which works but when I call it, it inserts the the code into the page I call it from as-well. Is there a way to stop this or a better way to achieve what I am after? function wwwcopy($dynamic,$static) { ob_start(); require $dynamic; $temp= ob_get_contents(); ob_end_flush(); $fh = fopen($static,"w"); fwrite($fh,$temp); fclose($fh); } Quote Link to comment https://forums.phpfreaks.com/topic/176984-solved-convert-dynamic-php-page-to-static-html/#findComment-934297 Share on other sites More sharing options...
Garethp Posted October 10, 2009 Share Posted October 10, 2009 It inserts code into $dynamic? It shouldn't, can you show us a before and after example? Quote Link to comment https://forums.phpfreaks.com/topic/176984-solved-convert-dynamic-php-page-to-static-html/#findComment-934299 Share on other sites More sharing options...
PFMaBiSmAd Posted October 10, 2009 Share Posted October 10, 2009 Use ob_end_clean(); (which clears the output buffer) instead of ob_end_flush(); (which flushes the output buffer to the browser.) Quote Link to comment https://forums.phpfreaks.com/topic/176984-solved-convert-dynamic-php-page-to-static-html/#findComment-934301 Share on other sites More sharing options...
isedeasy Posted October 10, 2009 Author Share Posted October 10, 2009 Use ob_end_clean(); (which clears the output buffer) instead of ob_end_flush(); (which flushes the output buffer to the browser.) Thank you, that did the job. Thanks for all the help guys Quote Link to comment https://forums.phpfreaks.com/topic/176984-solved-convert-dynamic-php-page-to-static-html/#findComment-934348 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.