【问题标题】:Laravel Storage not saving imageLaravel 存储不保存图像
【发布时间】:2021-05-24 12:20:26
【问题描述】:

我正在尝试将图像保存在存储中。这是我的代码

 public function uploadEducationalInstitutionLogo(Request $request)
    {
        if ($request->hasFile('logo')) {

            $file = $request->file('logo');
            $fileName = time() . $file->getClientOriginalName();

            $img = Image::make($file);

            $img->fit(820, 312, function (Constraint $constraint) {
                $constraint->upsize();
            });

            Storage::putFileAs('institution-logo', (string)$img->encode(null, 95), $fileName);

            return response()->json([
                'filename' => $fileName
            ]);
        }
    }

我使用 vue filepond 组件作为我的图片上传器来自 vue 前端的请求:

 <file-pond
    name="logo"
    allow-multiple="false"
    max-files="1"
    accepted-file-types="image/jpeg, image/png"
    v-bind:files="myFiles"
    :label-idle="label"
    v-on:init="handleFilePondInit"
    v-on:processfile="onload"
    :server="server"
  />
<script>
data() {
    return {
      myFiles: [],
      server: {
        url: `http://127.0.0.1:8000/api/logo`,
        process: {
          headers: {
            Authorization: localStorage.getItem("access_token"),
          },
        },
      },
    };
  },
<script>

这里抛出以下错误:

ErrorException: fopen() 期望参数 1 是有效路径,文件中给出的字符串...

这是我保存图像的 API 请求:

我想将我的图片存储在 storage/images/institution-logo/image.jpg

另一个问题是最好保存在哪里:

1) 存储/图像/.. 或
2)公共/存储/图像/...?

提前致谢!

【问题讨论】:

  • 您是否在表单中添加了enctype="multipart/form-data"
  • 请看我编辑的版本@BABAKASHRAFI
  • 请在问题中添加您的html表单
  • 你是通过 ajax(xhr) 请求发送的吗?
  • 请再次查看编辑后的版本

标签: laravel vue.js laravel-7 filepond laravel-storage


【解决方案1】:
$data = $request->all();
foreach ($data['logo'] as $imgFile) {
    $imgName = 'logo_' . random_int(111111, 999999) . '.' . $imgFile->getClientOriginalExtension();
    $imgFile->move(storage_path('/images/', $imgName);
}

使用 move(storage_path);

【讨论】:

  • 嘿@Gevorg,欢迎!我稍微整理了你的答案 - 如果你点击编辑,你会看到我如何使用反引号 (`) 来格式化代码以使其更易于阅读。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
相关资源
最近更新 更多