简体   繁体   中英

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 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标记缓冲输出应该从哪里开始,但是AFAIK你还必须告诉PHP结束缓冲并输出当前内容: ob_end_flush()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM