我在PHP页面中有以下代码。有时,当我删除cache2.html文件时,我希望php重新创建它,下一个人将得到cache2.html,而不是执行php代码。我收到以下警告在页面上几次,没有内容。这是因为多个用户同时访问php吗?如果是这样的话,我该如何解决呢?谢谢。
警告:包含(dir1 1/cache2.html) function.include:未能打开流:第8行/home/content/54/site/index.php中没有这样的文件或目录
<?php
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
$cachefile = "dir1/cache2.html";
if (file_exists($cachefile)) {
include($cachefile); // output the contents of the cache file
} else {
/* HTML (BUILT USING PHP/MYSQL) */
$cachefile = "dir1/cache2.html";
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_flush(); // Send the output to the browser
}
?>发布于 2012-06-04 19:44:51
对file_exists()的调用本身是缓存的,所以即使在文件被删除之后,也很可能会得到true的返回值。请参见:
http://us.php.net/manual/en/function.clearstatcache.php
所以,你可以:
clearstatcache();
if (file_exists($cache)) {
include($cache);
} else {
// generate page
}或者,您也可以这样做:
if (file_exists($cache) && @include($cache)) {
exit;
} else {
// generate page
}或者更好的是,如果要从PHP进程中删除缓存文件,那么只需在删除文件后调用clearstatcache()即可。
https://stackoverflow.com/questions/10887218
复制相似问题