2

ページをキャッシュするために、このチュートリアルhttp://papermashup.com/caching-dynamic-php-pages-easyly/を使用しています

<?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
}
?>

しかし、私は次のエラーが発生します

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

ファイルへのパスは正しいです。ファイルを編集すると、自分自身が含まれますが、再びエラーが発生します

4

4 に答える 4

6

変数名の大文字と小文字に問題があります。PHP 変数名は大文字と小文字が区別されます。(代わりに小さいFを使用) に変更cacheFileします。cachefile

これを変える:

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

これに:

$cached = fopen($cachefile, 'w');
于 2012-05-16T17:07:19.737 に答える
4

スペル ミスが発生しました: $cachefile!= $cacheFilePHP 識別子は大文字と小文字が区別されます。したがって、1 つのバージョンを決定し、他の発生を修正します。

修正されたコード:

$cached = fopen($cachefile, 'w');
于 2012-05-16T17:07:05.713 に答える
3

初めて参照します$cachefile。2回目に参照します$cacheFile。ケーシングをどこかで固定すれば、うまくいくはずです。

于 2012-05-16T17:07:14.597 に答える