Laravel 在存储中使用 storeAs() 存储图像问题。

huangapple go评论52阅读模式
英文:

Laravel storeAs() images in storage issue

问题

我在将图像上传到我的 Laravel 存储文件夹之一内的特定文件夹时遇到了问题。我尝试了几种方法,但它会创建新目录,而不是将图像保存在正确的目录 storage/app/public/images/schools/logos/ 中。

在我的文件系统配置文件中,我有以下设置:

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

我已经检查了权限,一切都正常,尽管不正确地创建了新目录并存储了图像,但并没有在所需的路径(文件夹)上存储。

我的 storage/ 目录结构如下:

我尝试使用以下代码行将图像上传到 storage/app/public/images/schools/logos/

$filename = 'f032ec59-d568-4107-86d8-97fad7986273.png';
$request->file($type)->storeAs('public/images/schools/logos', $filename);

我认为路径中的某些部分是不正确的,但尽管尝试了多次,我也无法弄清楚。(我以前习惯于存储在 /public/images,但现在正在将东西移到 storage 中。)

英文:

I'm having a problem uploading images to a specific folder inside one of my Laravel storage folders. I've tried several approaches, and it is creating new directories instead of saving the image in the correct directory, storage/app/public/images/schools/logos/.

In my filesystem config file, I have the following settings:

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

I've checked the permissions, and all are ok, even despite, wrongfully, it can create a new directory and store the image, yet not on the desired path (folder).

The directory structure in my storage/ is the following:

I've tried to upload to storage/app/public/images/schools/logos/ using the following line of code:

$filename = 'f032ec59-d568-4107-86d8-97fad7986273.png';
$request->file($type)->storeAs('public/images/schools/logos', $filename);

I presume something in the path is incorrect, but I can't figure it out despite several attempts. (I was used to storing in the /public/images but now moving things to storage.

答案1

得分: 1

如果您正在使用storeAs()方法,可以将磁盘名称作为该方法的第三个参数传递:

$filename = 'f032ec59-d568-4107-86d8-97fad7986273.png';
$request->file($type)->storeAs('images/schools/logos', $filename, 'public')
英文:

If you are using the storeAs() method, you may pass the disk name as the third argument to the method:

$filename = 'f032ec59-d568-4107-86d8-97fad7986273.png';
$request->file($type)->storeAs('images/schools/logos', $filename, 'public')

huangapple
  • 本文由 发表于 2023年6月26日 02:58:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定