首页 > 解决方案 > Laravel 5.7 - 存储链接路径不正确

问题描述

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

<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">

在控制台中。

在存储/图像中,我还可以看到所有上传的图像。但是,在我的文本编辑器中,存储和图像之间有一个“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(在文档中说明)。

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

提前谢谢大家!

标签: phplaravel

解决方案


如果有人正在寻找解决方案,这里有一个很好的提示:使用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();

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

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

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


推荐阅读