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

Link to comment
Share on other sites

        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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.