Can't get output to the screen using PHP?

I have the following scenario.

I have a PHP website that contains about 3 web pages with dynamic content. I want to pass this static content to another file. For example. 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 it doesn't work; -? (

+2


source to share


4 answers


require_once () does not return content outputted by the script. You need to get the output of the script, which 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 () :



Returns the current contents of the buffer and delete the current output buffer.

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

http://php.net/ob_get_clean

+9


source


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

      



+2


source


require_once

opens a file and tries to parse it like PHP. It will not return its result. What you are probably looking for is:

<?php
ob_start();
require_once('file.php');
$content = ob_get_contents();
ob_end_flush();
// etc...
?>

      

This way the script stores the data in $ content AND dumps it to standard output. If you only want $ content to be filled, use ob_end_clean()

instead ob_end_flush()

.

+2


source


Checkout ob_get_flush ( http://www.php.net/manual/en/function.ob-get-flush.php )

Basically try doing

if(!is_file("contact.html")){
  ob_start();
  require_once 'contact.php';
  $content = ob_get_flush();
  file_put_contents("contact.html", $content);
}else{
  echo file_gut_contents("contact.html");
}

      

This should buffer the output from contact.php

and flush it as needed.

+2


source







All Articles