【问题标题】:Laravel 5.7 - Storage Link incorrect pathLaravel 5.7 - 存储链接路径不正确
【发布时间】:2019-03-17 16:58:43
【问题描述】:

我目前通过文件输入存储图像,如下所示:

<input type="file" name="images" class="form-control">

在 store 函数中,我像这样存储文件和 url:

      $blog = new Blog;

      $path = Storage::putFile('image', $request->file('images'));

      $url = Storage::url($path);

      $blog->file = $url;
      $blog->save();

效果很好。作为 tinker 中的路径,我为每个博客 ($blog->file) 获得了以下响应:

"/storage/images/GmjAKA6FkId7vli2aw1oq2Z3MkAzZQRxGPoQeVud.jpeg"

要在视图部分显示图像,我使用:

<img src="asset/{{ $blog->file}}" alt="" class="card-img-top">

打印出来的:

<img src="asset/storage/images/GmjAKA6FkId7vli2aw1oq2Z3MkAzZQRxGPoQeVud.jpeg" alt="" class="card-img-top">

在控制台中。

在 storage/images 中,我还可以看到所有上传的图像。 但是,在我的文本编辑器中,存储和图像之间有一个“app”文件夹,如下所示:

"storage/app/images"

这就是没有图像显示的原因。

既然我使用的是 Storage.put,为什么 Storage 将路径保存为“storage/images/”而不是“storage/app/images”?

文件系统.php:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default filesystem disk that should be used
    | by the framework. The "local" disk, as well as a variety of cloud
    | based disks are available to your application. Just store away!
    |
    */

    'default' => env('FILESYSTEM_DRIVER', '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' => env('FILESYSTEM_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.
    |
    | Supported Drivers: "local", "ftp", "sftp", "s3", "rackspace"
    |
    */

    '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'),
            'url' => env('AWS_URL'),
        ],

    ],

];

我之前跑过php artisan link:storage(在文档中说明)。

如果有人知道是什么原因造成的,我会很高兴听到它!

提前谢谢大家!

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    如果有人正在寻找解决方案,这里有一个很好的提示:使用laravel-medialibrary

    Medialibrary 是一个 Laravel(5.6 及更高版本)包,可以将各种文件与 Eloquent 模型相关联。它提供了一个简单、流畅的 API 来使用。

    使用上面的示例,您可以简单地执行以下操作:

    $blog = Blog::find(1);
    $blog->addMedia($pathToFile)->toMediaCollection('images');
    

    或者,如果您想直接处理上传,例如问题中的情况:

    $blog->addMediaFromRequest('image')->toMediaCollection('images');
    

    要检索文件,您可以使用getMedia-方法。该方法返回Media-objects 的集合,换句话说,它返回给定模型的所有媒体:

    $mediaItems = $blog->getMedia();
    

    您可以使用getUrlgetPath 检索与Media 对象关联的文件的url 和路径:

    $publicUrl = $mediaItems[0]->getUrl();
    $publicFullUrl = $mediaItems[0]->getFullUrl(); //url including domain
    $fullPathOnDisk = $mediaItems[0]->getPath();
    

    在简历中,通过使用这个库,您无需直接担心任何存储配置,您可以通过调用模型本身的方法来完成所有工作。我强烈建议你看一下 Docs,它真的很容易使用。

    【讨论】:

    • 请进一步解释一下——那个库是什么,如何使用它来解决给定的问题?
    • @NicoHaase 对不起,我以为我添加了一个指向包文档的链接,我的错。我已经更新了我的答案以提供进一步的说明。
    【解决方案2】:

    感谢大家的回复!

    我想出了以下“解决方案”:

    我只是存储在公用文件夹中,通过:

      $path = Storage::putFile('public', $request->file('images'));
    

    看来 storage:link 只能暴露公用文件夹。

    【讨论】:

      【解决方案3】:

      公开存储目录不是一个好习惯,

      //保存为

      Storage::disk('local')->put('/dir_name/'.unique_file_name.extension, File::get(tmp_dir_received));
      

      它全局引用你的 storage/app 目录

      storage/app/dir_name 中检索文件内容。

      <img src="{{ url('/dir_name/'.$blog->file) }}" alt="" class="card-img-top">
      

      【讨论】:

      • php artisan storage:link 不公开存储文件夹;它使public/storagestorage/app/public 的符号链接
      • 我的参考不是 php artisan storage:link 而是问题中提到的“storage/app/images”目录。
      【解决方案4】:

      不是php artisan link:storage,而是php artisan storage:link

      您必须使用asset() 助手来获得存储图像的正确路径:

      &lt;img src="{{ asset($blog-&gt;file) }}" alt="" class="card-img-top"&gt;

      【讨论】:

        猜你喜欢
        • 2021-06-07
        • 1970-01-01
        • 2016-10-05
        • 1970-01-01
        • 2020-11-12
        • 2021-10-20
        • 1970-01-01
        • 1970-01-01
        • 2013-02-01
        相关资源
        最近更新 更多