Laravel检查存储是否存在不起作用。

6
我正在尝试通过以下方式检查存储中是否已存在文件:
if(Storage::exists($request->file_name)):
    dd(var_dump("it did exist"));
else:
    dd(var_dump("it did not exist"));
endif

但是,即使文件实际上已被删除或不存在,它总是返回一个字符串"it did exist"。你有什么想法或帮助吗?

我确定我的存储位置设置没有问题,因为上传文件到存��时没有任何问题。

Storage::put($file_name, File::get($file));

PS:我使用的是 Laravel 5.0

更新:

这里是我的 Laravel 5.0 配置文件夹中的 filesystems.php 文件。

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default filesystem disk that should be used
    | by the framework. A "local" driver, as well as a variety of cloud
    | based drivers are available for your choosing. Just store away!
    |
    | Supported: "local", "s3", "rackspace"
    |
    */

    'default' => 'local',

    /*
    |--------------------------------------------------------------------------
    | Default Cloud Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Many applications store files both locally and in the cloud. For this
    | reason, you may specify a default "cloud" driver here. This driver
    | will be bound as the Cloud disk implementation in the container.
    |
    */

    'cloud' => 's3',

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    |
    | Here you may configure as many filesystem "disks" as you wish, and you
    | may even configure multiple disks of the same driver. Defaults have
    | been setup for each driver as an example of the required options.
    |
    */

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root'   => storage_path().'/employee_documents/',
        ],

        's3' => [
            'driver' => 's3',
            'key'    => 'your-key',
            'secret' => 'your-secret',
            'region' => 'your-region',
            'bucket' => 'your-bucket',
        ],

        'rackspace' => [
            'driver'    => 'rackspace',
            'username'  => 'your-username',
            'key'       => 'your-key',
            'container' => 'your-container',
            'endpoint'  => 'https://identity.api.rackspacecloud.com/v2.0/',
            'region'    => 'IAD',
        ],

    ],

];

你确定在config filesystems.php中设置了正确的默认存储吗? - vijaykumar
@vijaykumar:是的,我确定,因为当我上传文件到存储时,没有任何错误或问题(文件已成功上传)。 - Juliver Galleto
不知道 @Code Demon。但是请分享你的filesystems.php文件,我会在我的电脑上检查一下。 - vijaykumar
@vijaykumar:请查看我的更新帖子。 - Juliver Galleto
$request->file_name 返回的是什么?你知道那里有一个值吗? - Daniel
显示剩余2条评论
4个回答

5
我在 Laravel 5.5 上遇到了同样的问题,解决方法是使用。
Storage::disk('public')->exists($file_name)

替代
Storage::exists($file_name)

在我的 config/filesystem.php 文件中,默认情况下有两种本地磁盘类型(local和public),我之前不知道这一点:

    'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],

],

1

我真的不知道为什么在你的情况下它不起作用。我在我的机器上尝试过,它可以正常工作。

我尝试调试 Storage::exits 中的内容,它使用相同的 return file_exists($path);

这可能是原因

file_exists() 的结果被缓存了

尝试以下步骤

  1. php artisan config:cache
  2. php artisan cache:clear
  3. php artisan view:clear
  4. chmod -R 777 storage/ 或 chown -R webuser:webuser storage/

在生产环境中绝对不要使用chmod -R 777。https://medium.com/@oliverjkb/why-chmod-777-is-internet-advice-from-hell-2fb4049f1137 - Bruno Francisco

1

这可能会对你有帮助。它可以与Laravel5.4一起使用:

Storage::exists('dir/'. $filename)

0
if(File::exists('filePath')){
     dd(var_dump("it did exist"));

}else{
   dd(var_dump("it did not exist"));
}

请尝试上面的代码。

我正在使用 Laravel 存储门面而不是文件门面。 - Juliver Galleto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接