Search code examples
phpcachingfopen

Warning: fopen() [function.fopen]: Filename cannot be empty in


Im using this tutorial http://papermashup.com/caching-dynamic-php-pages-easily/ for caching a page

<?php {
$cachefile = $_SERVER['DOCUMENT_ROOT'].'cache.html';
$cachetime = 4 * 60;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
    include($cachefile);
} else {
ob_start(); // Start the output buffer
 ?>

/* Heres where you put your page content */


<?php 
// Cache the contents to a file
$cached = fopen($cacheFile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
}
?>

but i get the following errors

Warning: fopen() [function.fopen]: Filename cannot be empty in

Warning: fwrite(): supplied argument is not a valid stream resource in

Warning: fclose(): supplied argument is not a valid stream resource in

The path to the file is right. And if i edit the file my self is included but again i get the errors


Solution

  • You have a problem with the casing of your variable name. PHP variable names are case sensitive. Change cacheFile to cachefile (with the small F instead).

    Change this:

    $cached = fopen($cacheFile, 'w');
    

    To this:

    $cached = fopen($cachefile, 'w');