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?

2
  • Did you check the rights on the log file's path? Commented Jul 22, 2010 at 11:04
  • 2
    typing mistake. :) $contents => $content Commented Jul 22, 2010 at 11:07

3 Answers 3

6

try

logit($content);
//           ^^ Note the missing s
Sign up to request clarification or add additional context in comments.

Comments

0

$contents is not the same variable as $content.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.