Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't render output to screen using PHP?

Tags:

php

I have the following scenario.

I have a PHP website witch contains about 3 webpages with dynamic content. I want to render this to static content to another file. Eg. contact.php -> contact.html

My code looks like this

ob_start();
$content = require_once 'contact.php';
ob_end_flush();
file_put_contents("contact.html", $content);

Somehow this is not working ;-?(

like image 702
Elitmiar Avatar asked May 02 '26 03:05

Elitmiar


1 Answers

require_once() doesn't return the content outputted by a script. You need to get the script output that is stored in the output buffer:

ob_start();
require_once('contact.php');
$content = ob_get_clean();

file_put_contents('contact.html', $content);

ob_get_clean():

Gets the current buffer contents and delete current output buffer.

ob_get_clean() essentially executes both ob_get_contents() and ob_end_clean().

http://php.net/ob_get_clean

like image 105
Tom Haigh Avatar answered May 03 '26 17:05

Tom Haigh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!