Php write buffer to file

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?

asked Jul 22, 2010 at 11:02

2

try

logit[$content];
//           ^^ Note the missing s

answered Jul 22, 2010 at 11:05

jigfoxjigfox

17.9k3 gold badges56 silver badges73 bronze badges

0

$contents is not the same variable as $content.

answered Jul 22, 2010 at 11:07

Ahmed AmanAhmed Aman

2,3431 gold badge19 silver badges33 bronze badges

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

answered May 17, 2018 at 13:02

cronokleecronoklee

6,0548 gold badges48 silver badges75 bronze badges

The stream_set_write_buffer[] function has been broken since PHP 4.3.0. As a workaround, I would suggest using a php_user_filter as an output buffer. By using the following class I have measured over 50% performance improvements with scripts that generate large text files by writing them one line at a time:

class my_output_buffer extends php_user_filter
{
    protected static $BUFFER_SIZE = 4096;

    function onCreate[ ] {
        $this->bff = [];
        $this->len = 0;
        return true;
    }

    public function filter[$in, $out, &$consumed, $closing]
    {
        $rv = PSFS_FEED_ME; /* assume no output */

        /* process input */
        while [$bucket = stream_bucket_make_writeable[$in]] {

            /* bucket is too big for the buffer? */
            $space = static::$BUFFER_SIZE - $this->len;

            if [$bucket->datalen >= $space] {
                /* consume data by placing it into internal buffers */
                $this->bff[] = substr[$bucket->data, 0, $space];
                $this->len += $space;
                $overflow = substr[$bucket->data, $space];
                $ovfl_len = $bucket->datalen - $space;
                $consumed += $bucket->datalen;

                assert[$this->len == static::$BUFFER_SIZE];

                /* make one big bucket */
                $bucket->data = implode['', $this->bff];
                $bucket->datalen = static::$BUFFER_SIZE;
                stream_bucket_append[$out, $bucket];
                $rv = PSFS_PASS_ON; /* we have output! */

                /* handle overflow */
                $this->bff = [$overflow];
                $this->len = $ovfl_len;
            }
            else {
                /* consume data by placing it into internal buffers */
                $this->bff[] = $bucket->data;
                $this->len += $bucket->datalen;
                $consumed += $bucket->datalen;
            }
        }

        /* stream is closing and we have data? */
        if [$closing && $this->len > 0] {
            /* make one last bucket */
            $data = implode['', $this->bff];
            $bucket = stream_bucket_new[$this->stream, $data];
            stream_bucket_append[$out, $bucket];
            $rv = PSFS_PASS_ON; /* we have output! */

            /* clear internal buffer */
            $this->bff = [];
            $this->len = 0;
        }

        return $rv;
    }
}

$fp = fopen['foobar.txt', 'w'];

/* enable filtering */
stream_filter_register['output.buffer', 'my_output_buffer'];
stream_filter_append[$fp, 'output.buffer'];

/* a lot of small writes */
for [$i = 0; $i < 10000; $i++] {
    fwrite[$fp, 'x'];
}

fclose[$fp];

Chủ Đề