由于蛋糕壳,CakePHP 2.x警告SplFileInfo

时间:2014-04-23 17:31:36

标签: php shell cakephp cakephp-2.0 command-line-interface

我写了一个Cakehell脚本,我打算用cronjob来使用它。在手动运行时(测试期间),有时我的网站会抛出SplFileInfo警告,例如:

Warning: SplFileInfo::openFile(/var/www/flat/app/tmp/cache/persistent/myapp_cake_core_file_map): 
failed to open stream: Permission denied in /var/www/flat/lib/Cake/Cache/Engine/FileEngine.php on line 313

Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/models/myapp_cake_model_default_flat_list): 
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]

Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/models/myapp_cake_model_default_flat_list) [http://php.net/splfileinfo.openfile]: 
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]

Warning (512): _cake_model_ cache was unable to write 'default_flat_list' to File cache [CORE/Cake/Cache/Cache.php, line 309]

Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/persistent/myapp_cake_core_method_cache) [http://php.net/splfileinfo.openfile]: 
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]

Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/persistent/myapp_cake_core_method_cache) [http://php.net/splfileinfo.openfile]: 
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]

Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/models/myapp_cake_model_default_flat_list) [http://php.net/splfileinfo.openfile]: 
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]

Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/models/myapp_cake_model_default_flat_list) [http://php.net/splfileinfo.openfile]: 
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]

Warning (512): _cake_model_ cache was unable to write 'default_flat_list' to File cache [CORE/Cake/Cache/Cache.php, line 309]

发生什么事了?以及如何解决这个问题?

如果我检查缓存上的tmp目录并且持久化某些文件是否具有root权限。这是原因吗?

[root@Apps103 persistent]# ls
total 40K
-rw-rw-r-- 1 apache   43 Apr 22 17:49 myapp_cake_core_cake_
-rw-rw-r-- 1 root     43 Apr 23 10:01 myapp_cake_core_cake_console_
-rw-rw-r-- 1 root     43 Apr 23 10:01 myapp_cake_core_cake_dev_
-rw-rw-r-- 1 apache   43 Apr 23 10:26 myapp_cake_core_cake_dev_en-us
-rw-rw-r-- 1 apache   43 Apr 23 10:26 myapp_cake_core_cake_en-us
-rw-rw-r-- 1 apache   43 Apr 23 10:26 myapp_cake_core_default_en-us
-rw-rw-r-- 1 root   4.3K Apr 23 10:01 myapp_cake_core_file_map
-rw-rw-r-- 1 root   4.3K Apr 23 10:01 myapp_cake_core_method_cache

我尝试了此链接SplFileInfo::openFile(/app/tmp/cache/persistent/cake_core_cake_console_):failed to open stream:Permission denied in /lib/.../FileEngine.php line 293

中的解决方案

我把它放在bootstrap中

Cache::config('default', array(
    'engine' => 'File',
    'mask' => 0666,
));

// short  
Cache::config('short', array(
    'engine' => 'File',
    'duration' => '+1 hours',
    'path' => CACHE,
    'prefix' => 'cake_short_'
));

// long
Cache::config('long', array(
    'engine' => 'File',
    'duration' => '+1 week',
    'probability' => 100,
    'path' => CACHE . 'long' . DS,
));

但它不起作用。通常我会清理整个tmp目录以删除警告。然后它会再次正常工作。我不确定原因,但如果我运行shell,则警告错误不会显示,直到第二天。 我的tmp目录(flat / app / tmp)受到apache权限。

1 个答案:

答案 0 :(得分:4)

发生了什么事?

tmp文件夹通常位于app dir中,无论来自哪个请求都可以共享。一个相对常见的问题是通过Web请求创建的tmp文件对于控制台任务是只读的,反之亦然。

如果以root用户身份运行cli任务,则将使用所有者root创建tmp文件 - 此后webrequest(由apache运行)可能会尝试从tmp目录中删除或写入但无法修改现有tmp文件夹文件由root拥有的内容。

如何解决这个问题?

使cli和web请求使用不同的 tmp目录 - 或者只是确保cli和web用户都具有对tmp目录的写权限。

这意味着要么将TMP常量before CakePHP does定义到app / tmp以外的地方,例如在cake.php中:

...
$ds = DIRECTORY_SEPARATOR;
define('TMP', '/tmp/myapp');
...

或确保tmp文件夹对两个用户都可写,例如:

cd app
chmod -R 777 tmp