Jump to content

Why does ob_get_flush() return an empty string?


tmallen

Recommended Posts

The cache file is created, but it's empty. I can pass in a debug string which is written successfully.

<?php
function page($name, $params = array(), $cache = false) {
    if(is_array($params)) {
        extract($params);
    } else {
        $title = $params;
    }

    function put_page($name) {
        ob_start();
        include sprintf('pages/%s.php', $name);
        $content = ob_get_clean();
        include 'pages/page.php';
        return ob_get_flush();
    }

    if($cache) {
        $cache_file = sprintf('cache/page/%s.html', $name);

        if(file_exists($cache_file)) {
            echo file_get_contents($cache_file);
            unlink($cache_file);
        } else {
            if(!is_dir('cache/page')) {
                mkdir('cache/page');
            }
            $cached = fopen($cache_file, 'w');
            fwrite($cached, put_page($name));
            fclose($cached);
        }
    } else {
        put_page($name);
    }
}

        ob_start();

        include sprintf('pages/%s.php', $name);

        $content = ob_get_clean();

        include 'pages/page.php';

        return ob_get_flush();

 

You must decide what you want to do. You use ob_get_flush to put the buffer into $content then expect return ob_get_flush to give you something from an empty buffer. Either return from the function or the contents of the buffer into $contents, but not both

 

 

HTH

Teamatomic

flush is the term. ob_get_flush flushes the buffer contents to a var, ob_put_contents flushes the buffer to page output. How can the include add to the buffer after it has already added and been flushed? if that is what you want you need to first start a new buffer.

 

ob_start();

        include sprintf('pages/%s.php', $name);

        $content = ob_get_clean();

ob_start();

        include 'pages/page.php';

        return ob_get_flush();

 

 

HTH

Teamatomic

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.