Laravel试图在根文件夹中存储缓存文件


Laravel Trying to Store Cache Files in Root Folder

在渲染任何页面时,我从Laravel5获得以下错误:

ErrorException in Filesystem.php line 74: 
file_put_contents(/fdcc73e2e72031a510ae8f921ce1d22d): failed to open stream: Permission denied

我的缓存配置如下:

<?php
return [
    /*
    |--------------------------------------------------------------------------
    | Default Cache Store
    |--------------------------------------------------------------------------
    |
    | This option controls the default cache connection that gets used while
    | using this caching library. This connection is used when another is
    | not explicitly specified when executing a given caching function.
    |
    */
    'default' => env('CACHE_DRIVER', 'file'),
    /*
    |--------------------------------------------------------------------------
    | Cache Stores
    |--------------------------------------------------------------------------
    |
    | Here you may define all of the cache "stores" for your application as
    | well as their drivers. You may even define multiple stores for the
    | same cache driver to group types of items stored in your caches.
    |
    */
    'stores' => [
        'apc' => [
            'driver' => 'apc'
        ],
        'array' => [
            'driver' => 'array'
        ],
        'database' => [
            'driver' => 'database',
            'table'  => 'cache',
            'connection' => null,
        ],
        'file' => [
            'driver' => 'file',
            'path'   => storage_path().'/framework/cache',
        ],
        'memcached' => [
            'driver'  => 'memcached',
            'servers' => [
                [
                    'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100
                ],
            ],
        ],
        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],
    ],
    /*
    |--------------------------------------------------------------------------
    | Cache Key Prefix
    |--------------------------------------------------------------------------
    |
    | When utilizing a RAM based store such as APC or Memcached, there might
    | be other applications utilizing the same cache. So, we'll specify a
    | value to get prefixed to all our keys so we can avoid collisions.
    |
    */
    'prefix' => 'laravel',
];

有趣的是,如果我将默认值更改为array或任何其他选项,它仍然尝试将缓存文件写入根文件夹并且每次都失败。

还有其他人遇到过这个问题吗?

查看storage/framework/views是否存在。如果你把它加进去,还是会有问题。确保它是可写的(775、77等)

在我的例子中,发生错误是因为我通过github部署到生产服务器,但是存储的内容没有被git跟踪。我通过确保/storage中每个目录中的.gitignore文件都提交到repo来解决这个问题。

指出:通常那些奇怪的文件名与Blade模板有关。我不认为它实际上试图将缓存文件存储在根目录下。

我的错误是类似的:"ErrorException in filessystem .php line 81: file_put_contents(/f946048111f1176619e22f5127724e37):未能打开流:Permission denied"

我用的是debian 7 wheezy。

如果您遇到这些类型的错误,请执行以下步骤

进入app文件夹(例如:/home/app/web)

  1. php artisan view:clear
  2. chmod -R 777 storage/或chown -R webuser:webuser storage/
  3. chmod -R 777 public/or chmod -R webuser:webuser public/
  4. service apache2 reload,service apache2 restart

注:这里的web用户指的是www-data或定制用户

请确保数据库与页面连接。

你从哪里加载CACHE_DRIVER ?

'default' => env('CACHE_DRIVER', 'file'),

查找系统环境变量和。env文件位于项目的根目录。根据您所描述的方法,我假设您只是试图更改缓存配置文件中的缓存方法。如果laravel没有在其他地方找到CACHE_DRIVER,则该方法的第二个参数是一个'默认'值。

您应该检查文件。在项目的根目录下为变量CACHE_DRIVER设置env。如果存在,则该值将优先于cache.php上的配置。


另一个解决方案是确保laravel在指定的目录上有写权限。查看该文件夹是否有www-data用户或组(至少)的写权限