7

I am having trouble with an update script. It runs for a few hours so I would like it to output live to a text file.

I start the document with

ob_start();

Then within the while loop (as it iterates through the records of the database) I have this

$size=ob_get_length();
if ($size > 0)
{
    $content = ob_get_contents();
    logit($contents);
    ob_clean();
}

And finally the logit function

function logit($data)
{
    file_put_contents('log.txt', $data, FILE_APPEND);
}

However the log file remains empty. What am I doing wrong?

Pablo
  • 4,273
  • 7
  • 33
  • 34

3 Answers3

6

try

logit($content);
//           ^^ Note the missing s
jigfox
  • 18,057
  • 3
  • 60
  • 73
0

$contents is not the same variable as $content.

Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
Ahmed Aman
  • 2,373
  • 1
  • 19
  • 33
0

For anyone coming here looking for a function for this, I wrote a nice one today:

//buffer php outout between consecutive calls and optionally store it to a file:
function buffer( $toFilePath=0, $appendToFile=0 ){
    $status = ob_get_status ();
    if($status['level']===1) return ob_start(); //start the buffer
    $res = ob_get_contents();
    ob_end_clean();
    if($toFilePath) file_put_contents($toFilePath, $res, ($appendToFile ? FILE_APPEND : null));
    return $res;
}

Sample usage:

buffer(); //start the buffer

echo(12345); //log some stuff
echo(678910);

$log = buffer('mylog.txt',1); //add these lines to a file (optional)

echo('Heres the latest log:'.$log);
cronoklee
  • 6,482
  • 9
  • 52
  • 80