laravel - 我无法在公共 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);
}
解决方案
首先,欢迎来到 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。
这就是为什么,除非在您的生产环境中您没有创建符号链接的能力,否则将文件直接保存到公用文件夹是没有用的,并且在大多数情况下是不安全的。
推荐阅读
- tfs - 如何使用 REST API 修改工作项的 BoardColumn 字段
- php - 如何在 Avada 子主题功能中覆盖 Fusion Element Container?
- c# - 通过 AD 服务主体进行 Azure SQL 身份验证
- python - 当列名在值中时,Pandas 拆分 DataFrame
- c++ - 通过本机 C++ 代码移动 UWP 应用程序窗口
- apache-beam - 如何在 Beam SQL shell 中添加 google cloud pubsub 作为源?
- sql - 更改表以增量添加 ID 值为空的主键记录
- python - 如何选择性地覆盖 Python 中的“==”行为?
- android - 如何从android NDK中的另一个c ++文件中读取常量
- python - 如何检索我刚刚上传的视频的 URL?