Kenttan Posted April 20, 2012 Share Posted April 20, 2012 Hi all, I need some help. My ob_get_contents() is empty and gives me a empty file everytime. I tried different cache script it is still the same. I have also tried fwrite($fp, 'hello') In this case, it successfully writes to the file. But when i use ob_start and ob_get_contents, it will be empty. can anyone please advice. My simplified script is here <?php // Settings $cachedir = 'cache/'; // Directory to cache files in (keep outside web root) $cachetime = 600; // Seconds to cache files for $cacheext = 'html'; // Extension to give cached files (usually cache, htm, txt) // Ignore List $ignore_list = array( '/rss.php', '/search/' ); // Script $page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page $cachefile = $cachedir . md5($page) . '.' . $cacheext; // Cache file to either load or create $ignore_page = false; for ($i = 0; $i < count($ignore_list); $i++) { $ignore_page = (strpos($page, $ignore_list[$i]) !== false) ? true : $ignore_page; } $cachefile_created = ((@file_exists($cachefile)) and ($ignore_page === false)) ? @filemtime($cachefile) : 0; @clearstatcache(); // Show file from cache if still valid if (time() - $cachetime < $cachefile_created) { //ob_start('ob_gzhandler'); @readfile($cachefile); //ob_end_flush(); exit(); } // If we're still here, we need to generate a cache file ob_start(); ?> All My HTML AND PHP CODES TO RUN THE SITE <?php // Now the script has run, generate a new cache file $fp = @fopen($cachefile, 'w'); // save the contents of output buffer to the file @fwrite($fp, ob_get_contents()); @fclose($fp); ob_end_flush(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/261305-ob_get_contents-give-empty-string/ Share on other sites More sharing options...
kicken Posted April 20, 2012 Share Posted April 20, 2012 Remove all your @-sign prefixes so you can see any errors that are being generated. You should almost never use @ in your code like that. Make sure you have error reporting enabled as well. At the top of the script put: error_reporting(-1); ini_set('display_errors', 'On'); Quote Link to comment https://forums.phpfreaks.com/topic/261305-ob_get_contents-give-empty-string/#findComment-1339032 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.