首页 > 解决方案 > 我无法在公共 laravel 中存储上传的图像

问题描述

我想上传一些图片并将它们存储在 public/ 但它存储在 storage/app/public

我以前用过 php artisan link:storage

这是代码:

public function store(Request $request)
    {
        $new_file_data=[
            'small_explain'=>$request->input('small_explain'),
            'title'=>$request->input('title'),
            'paragraph_one'=>$request->input('paragraph_one'),
            'paragraph_two'=>$request->input('paragraph_two'),
            'paragraph_list'=>$request->input('paragraph_list'),
            'paragraph_three'=>$request->input('paragraph_three'),
            'important_body'=>$request->input('important_body'),
            'quote'=>$request->input('quote'),
            'author_quote'=>$request->input('author_quote'),

             //image storage

            'index_image' => $request->file('index_image')->store('/public/Images'),
            'header_image' => $request->file('header_image')->store('/public/Images' ),
            'text_image' =>$request->file('text_image')->store('/public/Images'),

        ];
        Article::create($new_file_data);

    }

标签: laravel

解决方案


首先,欢迎来到 StackOverflow。

我认为您应该更深入地查看官方文档

文件系统配置文件位于config/filesystems.php. 在此文件中,您可以配置所有“磁盘”。每个磁盘代表一个特定的存储驱动程序和存储位置。

这是你的问题。在您的filesystems.php文件中,您有如下配置(除非您进行了任何更改):

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    */

    'default' => env('FILESYSTEM_DRIVER', 'local'),

    // [...]

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    */

    'disks' => [

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

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

    ],

];

使用此配置,Laravel 将作为您存储文件夹的根目录storage/app并包含您的自定义路径(在本例中storage/app/public/Images)。如果要在公用文件夹中保存某些内容,请以这种方式更改配置。

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    */

    'default' => 'public', // <-- Set the default disk

    // [...]

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    */

    'disks' => [

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

        'public' => [
            'driver' => 'local',
            'root' => public_path(),    // <-- Change the default path to the public folder
            'url' => env('APP_URL'),    // <-- And the default URL
            'visibility' => 'public',
        ],

    ],

];

无论如何,这不是最佳做法。正如您所写,您还使用了php artisan storage:link创建从storage/app/public文件夹到public/storage路径的符号链接的命令。这样,存储在该文件夹中的所有文件都将通过asset('storage/my-public-image.jpg').

此外,Laravel 有它的原生功能 $url = Storage::url('file.jpg');,它会根据你设置的配置(磁盘配置中的url参数)自动创建一个 URL。

这就是为什么,除非在您的生产环境中您没有创建符号链接的能力,否则将文件直接保存到公用文件夹是没有用的,并且在大多数情况下是不安全的。


推荐阅读