首页 > 解决方案 > Laravel 5.0 使用自定义 Filsystem 将图像保存为损坏或损坏

问题描述

我在使用 ajax 上传后保存图像,但图像被保存为损坏。

我的磁盘

'blockcontentimages' => [
    'driver' => 'local',
    'root'   => public_path() . '/static/core/img/templates',
],

我如何存储我的图像:

$image = $request->file('content');

$file = $image->getClientOriginalName();
$fileName = '/static/core/img/templates/' . $blockNewsletterPivotId . '_' . str_replace(' ', '_', $file);

Storage::disk('blockcontentimages')->put($fileName, $image);

$image 的 dd:

UploadedFile {#27
  -test: false
  -originalName: "cloud.jpg"
  -mimeType: "image/jpeg"
  -size: 54754
  -error: 0
}

我不确定我在这里做错了什么,为什么它被保存为损坏或损坏的文件。

编辑

当我尝试打开保存的文件时,我在照片查看器中收到此错误消息:

Windows 照片查看器无法打开此图片,因为该文件似乎已损坏、损坏或太大。

标签: phplaravel

解决方案


您必须使用file_get_contents方法才能获取图像

试试看:

Storage::disk('blockcontentimages')->put($fileName, file_get_contents($image));

推荐阅读