这是一个令人头疼的问题。我继承了一个 AngularJS 项目,该项目在服务器端由 PHP 支持。我的任务是编写一个小数据缓存函数,这非常简单。不幸的是,就在那时,前端出现了问题。在继续之前,我将展示我的 PHP 代码,以便我可以指出一些事情。由于 NDA,文件结构和其他各种名称发生了更改,并且删除了与问题无关的代码:
define('ROOT', dirname(__FILE__));
require('config/config.php');
require('includes/includes.php');
// caching
function cacheCategories()
{
$dirPath = ROOT . '/cache/categories';
$cacheFileName = '/categories.json';
$cacheFilePath = $dirPath . $cacheFileName;
$cacheTime = 86400;
if (file_exists($cacheFilePath) && (time() - $cacheTime) < filemtime($cacheFilePath)) { // cache file exists and is fresh
$cachedJSON = readfile($cacheFilePath);
if ($cachedJSON === false) {
throw new Exception('Could not read from cache file');
}
} else {
include_once('/site/load.php');
$categories = load_categories_tree();
$cachedJSON = json_encode($categories);
if (!is_dir($dirPath)) {
mkdir($dirPath);
}
$fp = fopen($cacheFilePath, 'w');
if ($fp === false) {
throw new Exception('Could not open cache file');
}
if (flock($fp, LOCK_EX)) {
$writeResult = fwrite($fp, $cachedJSON);
if ($writeResult === false) {
throw new Exception('Could not write to cache');
}
flock($fp, LOCK_UN);
fclose($fp);
}
}
return $cachedJSON;
}
global $page;
$page = new Page('');
$page->setDefaultLanguage('en');
echo cacheCategories();
如您所见,这非常简单。如果缓存文件存在并且是最新的,则只需从中加载数据即可。如果没有,则创建一个新的缓存文件。如有必要,会创建缓存目录。
我的问题是,当 AngularJS 尝试从这个 PHP 文件中获取数据时,xdebug 会生成一个关于
mkdir
的警告,并且我还得到一个通用的 Exception
:
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mkdir(): No such file or directory in /home/mp/www/tiles/api_categories.php on line <i>39</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0001</td><td bgcolor='#eeeeec' align='right'>241456</td><td bgcolor='#eeeeec'>{main}( )</td><td title='/home/mp/www/tiles/api_categories.php' bgcolor='#eeeeec'>../api_categories.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0015</td><td bgcolor='#eeeeec' align='right'>715400</td><td bgcolor='#eeeeec'>cacheCategories( )</td><td title='/home/mp/www/tiles/api_categories.php' bgcolor='#eeeeec'>../api_categories.php<b>:</b>67</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>3.8601</td><td bgcolor='#eeeeec' align='right'>54575184</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mkdir' target='_new'>mkdir</a>
( )</td><td title='/home/mp/www/tiles/api_categories.php' bgcolor='#eeeeec'>../api_categories.php<b>:</b>39</td></tr>
</table></font>
<pre>exception 'Exception' with message 'Could not open cache file' in /home/mp/www/tiles/api_categories.php:45
Stack trace:
#0 /home/mp/www/tiles/api_categories.php(67): cacheCategories()
#1 {main}</pre>
奇怪的是,当我直接访问 PHP 文件时,没有弹出警告、错误或异常。缓存文件在我第一次访问时正确创建,并在后续访问中正确读取。
当我在 PhpStorm 中设置零配置 PHP 调试时,此问题开始出现。在此之前,该项目运行良好。这几乎就像 Angular 正在抓取旧警告或其他内容的缓存版本,因为,当我直接访问 PHP 文件时,它会按预期工作。
有什么想法吗?我是 Angular 的新手,有什么办法可以清除它可能拥有的任何缓存吗?
您正在创建绝对路径:
$dirPath = '/cache/categories';
$cacheFileName = '/categories.json';
$cacheFilePath = $dirPath . $cacheFileName;
产生
/cache/categories/categories.json
^---root of your filesystem
您正在尝试执行相当于创建
c:\cache\categories\categories.json
的操作,而不是创建 c:\path\to\your\site\cache\categories.json
。
请记住,PHP 在文件系统级别运行。从 Web 角度来看,它不了解您网站的结构。
/...
对于 PHP 来说是“硬盘根目录”,而不是“站点根目录”。
想通了。就像有人在对我的问题的显然现已删除的评论中所说的那样,
is_dir()
缓存了之前失败状态的结果。调用 clearstatcache()
就可以清除它。