开发者

PHP ob_start not working

I am new to PHP and Code Ignitor, Facing some issue while trying to convert dynamic data content into static html file. This is code snippet. when i request code snippet file it prints only Error 111111111 nothing else. not able to understand what is the error here.

This is my original Code and here am trying to generate static html file with dynamic content. It doesn't works开发者_JAVA技巧 for me

<?php
    echo "Error 111111111";
    ob_start();
    $fileName = "sample.html";
?>
<html>
<body>
    Some html is here       
</body>
</html>
<?php
    try{ 
           $output = ob_get_contents(); // get contents of trapped output

            //write to file, e.g.
           $newfile = $fileName; 
           $file = fopen ($newfile, "w"); 
           fwrite($file, $output); 
           fclose ($file);  
           ob_end_clean(); // discard trapped output and stop trapping
    }catch (Exception $ex){                
      echo "Error ".$ex->getMessage();
    }      

?>


I don't see an error?

ob_start() suppresses all output until ob_flush() is called. You're not calling ob_flush(), so nothing after the ob_start() will be output. That's what you're seeing, and that's exactly the way it's supposed to work.

I suppose the real question is what were you trying to achieve?

The code snippet is quite confusing, because ob_start() doesn't generate any exceptions, yet you've put it into a try / catch block. Your catch section will never be called because nothing in the try block will ever generate any exceptions.

So what were you trying to do here? The answer to that may help us give you more guidance.


ob_start marks where buffered output should begin, but AFAIK you also have to tell PHP to end buffering and output the current contents: ob_end_flush()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜