Laravel 5.7-存储链路路径不正确



Im当前通过文件输入存储图像,如

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

在存储功能内部,我存储文件&网址是这样的:

$blog = new Blog;
$path = Storage::putFile('image', $request->file('images'));
$url = Storage::url($path);
$blog->file = $url;
$blog->save();

Whichs很管用。作为修补程序中的一个路径,我为每个博客($blog->文件)获得以下响应:

"/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/app/images"

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

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

Filesystems.hp:

<?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(如文档中所述)。

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

提前感谢大家!

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

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

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

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

或者,如果你想直接处理上传,就像问题中的情况一样:

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

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

$mediaItems = $blog->getMedia();

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

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

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

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

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

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

公开存储目录不是一个好做法

//使用保存

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

它全局引用您的存储/应用程序目录

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

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

感谢大家的回复!

我想出了以下"解决方案":

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

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

storage:link似乎只能公开公用文件夹。