CakePHP 2.x warning SplFileInfo due to cakeshell

空扰寡人 提交于 2019-12-10 17:36:02

问题


I wrote a cakeshell script which I plan to use it using cronjob. while having run it manually (during testing), sometimes my site will throw SplFileInfo Warning e.g:

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]

What happened? and how to fix this?

If I check the tmp directory on cache and persistent some of the file are under root permissions. Is this the cause?

[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

I tried the solution from this link SplFileInfo::openFile(/app/tmp/cache/persistent/cake_core_cake_console_):failed to open stream:Permission denied in /lib/.../FileEngine.php line 293

I put this in 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,
));

but it does not work. Usually I clean up the whole tmp directory to remove the warning. then it will work fine again. I'm not sure why, but if I ran the shell, the warning error does not show, until the next day. My tmp directory (flat/app/tmp) is under apache permission.


回答1:


What happened?

The tmp folder is normally in the app dir and shared for all requests no matter the origin. A relatively common problem is for tmp files created via a web request to be read-only for console tasks and vice-versa.

If you run cli tasks as root, the tmp files will be created with the owner root - thereafter a webrequest (running by apache) may try to delete from or write to the tmp directory but be unable to modify the existing tmp folder contents as the files are owned by root.

how to fix this?

Either make cli and web requests use different tmp directories - or simply ensure that both cli and web users have write permissions to the tmp directory.

That means either defining the TMP constant before CakePHP does to a place other than app/tmp, e.g. in cake.php:

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

Or ensure the tmp folder is writable to both users, e.g.:

cd app
chmod -R 777 tmp


来源:https://stackoverflow.com/questions/23251362/cakephp-2-x-warning-splfileinfo-due-to-cakeshell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!