简体   繁体   English

尝试在Laravel 5.8中上传图像数组时,仅上传最后一张图像

[英]Only last image is uploading while trying to upload an array of images in Laravel 5.8

I'm trying to upload an array of objects to my laravel db, one of the properties of the objects in the array (backgroundImgRaw) has a file. 我正在尝试将一个对象数组上载到我的laravel数据库中,该数组中对象的属性之一(backgroundImgRaw)有一个文件。

I have to loop through the array to upload all images and get the file name so as to save to db. 我必须遍历数组以上传所有图像并获取文件名,以便保存到db。 But only the last file gets uploaded and the name is returned for all iteration. 但是只有最后一个文件被上传,并且所有迭代都返回名称。

DB::transaction(function () use ($request) {
...

foreach ($request->slides as $slides) {
   $backgroundImg = null;

   if (isset($slides['backgroundImgRaw'])) {
       $img_ext = $slides['backgroundImgRaw']->getClientOriginalExtension();
       $img_name = Str::slug($request->user['name'], '-') . '_img_' . time() . '.' . $img_ext;
       $slides['backgroundImgRaw']->storeAs('public/images/' . $request->user['email'], $img_name);
       $backgroundImg = env('CUSTOM_URL', false) . '/storage/images/' . $request->user['email'] . '/' . $img_name;
       Log::info(json_encode($backgroundImg->getoriginalfilename(), JSON_PRETTY_PRINT));
   } else {
       $backgroundImg = $slides['backgroundImg'];
   }
}

$slide = Slide::create([
   'slide_type' => isset($slides['subType']) ? $this->getSlidetype($slides['subType']) : null,
   'layout_type' => $slides['type'],
   'survey_id' => $request->surveyId,
   'background_color' => isset($slides['backgroundColor']) ? $slides['backgroundColor'] : null,
   'background_imageURL' =>  $backgroundImg,
   'screenshot' => $slidescreenshot,
   'text_color' => isset($slides['textColor']) ? $slides['textColor'] : null,
   'button_background' => isset($slides['buttonBackground']) ? $slides['buttonBackground'] : null,
   'button_text_color' => isset($slides['buttonTextColor']) ? $slides['buttonTextColor'] : null,
   'main_text' => $mainText,
   'button_text' => isset($slides['btnText']) ? $slides['btnText'] : null
]);

...
}

Only the last image is uploaded. 仅上传最后一张图像。 But the rest of the create function works perfectly. 但是其余的create函数可以完美地工作。

$request->slides returns an array in this format $request->slides以这种格式返回一个数组

[
    {
        "id": "1",
        "type": "2",
        "backgroundColor": "#203661",
        "backgroundImg": "blob:http:\/\/localhost:3000\/4089c09c-caeb-4121-a2a3-9d7aaf20264d",
        "backgroundImgRaw": {},
    ...
    },
    {
        "id": "3",
        "type": "1",
        "backgroundImg": "blob:http:\/\/localhost:3000\/bc13e931-cb0a-425a-b3c6-2db6dbbdd5a4",
        "backgroundImgRaw": {},
    ...
    }
]

For anyone having a similar issue the problem is with the $img_name variable, time() goes down to the second so all the images were processed within the same second, so new images just keep overwriting previous images. 对于任何有类似问题的人,问题都在于$img_name变量,time()下降到秒,因此所有图像都在同一秒内处理,因此新图像只会覆盖以前的图像。 I have updated the line to: 我已将行更新为:

$img_name = Str::slug($request->user['name'], '-') . '_img_' . str_random(10) . '.' . $img_ext;

Only last image is saving because you put the create outside the foreach loop. 仅保存最后一张图像,因为您将create放置在foreach循环之外。 You must put it inside the loop. 您必须将其放入循环中。

I also noticed that you are using time() to generate random image name. 我还注意到您正在使用time()生成随机图像名称。 This is not the ideal way to do it because your code is executing so fast that all of the slide is generating the same time() . 这不是理想的方法,因为您的代码执行得如此之快,以至于所有幻灯片都生成相同的time() You can use Laravel's Str::random function. 您可以使用Laravel的Str::random函数。

Here are my suggested code: 这是我建议的代码:

use Illuminate\Support\Str;
...
DB::transaction(function () use ($request) {
...

foreach ($request->slides as $slides) {
   $backgroundImg = null;

   if (isset($slides['backgroundImgRaw'])) {
       $img_ext = $slides['backgroundImgRaw']->getClientOriginalExtension();
       $img_name = Str::slug($request->user['name'], '-') . '_img_' . Str::random(6) . '.' . $img_ext;
       $slides['backgroundImgRaw']->storeAs('public/images/' . $request->user['email'], $img_name);
       $backgroundImg = env('CUSTOM_URL', false) . '/storage/images/' . $request->user['email'] . '/' . $img_name;
       Log::info(json_encode($backgroundImg->getoriginalfilename(), JSON_PRETTY_PRINT));
   } else {
       $backgroundImg = $slides['backgroundImg'];
   }

   $slide = Slide::create([
      'slide_type' => isset($slides['subType']) ? $this->getSlidetype($slides['subType']) : null,
      'layout_type' => $slides['type'],
      'survey_id' => $request->surveyId,
      'background_color' => isset($slides['backgroundColor']) ? $slides['backgroundColor'] : null,
      'background_imageURL' =>  $backgroundImg,
      'screenshot' => $slidescreenshot,
      'text_color' => isset($slides['textColor']) ? $slides['textColor'] : null,
      'button_background' => isset($slides['buttonBackground']) ? $slides['buttonBackground'] : null,
      'button_text_color' => isset($slides['buttonTextColor']) ? $slides['buttonTextColor'] : null,
      'main_text' => $mainText,
      'button_text' => isset($slides['btnText']) ? $slides['btnText'] : null
   ]);
}

...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM