Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 尝试在Laravel 5.8中上载一组图像时,仅上载最后一个图像_Php_Laravel - Fatal编程技术网

Php 尝试在Laravel 5.8中上载一组图像时,仅上载最后一个图像

Php 尝试在Laravel 5.8中上载一组图像时,仅上载最后一个图像,php,laravel,Php,Laravel,我试图将一个对象数组上传到我的laravel db,数组中对象的属性之一(backgroundImgRaw)有一个文件 我必须在数组中循环以上载所有图像并获取文件名,以便保存到db。但是只有最后一个文件被上传,并且所有迭代都会返回名称 DB::transaction(function () use ($request) { ... foreach ($request->slides as $slides) { $backgroundImg = null; if (isset

我试图将一个对象数组上传到我的laravel db,数组中对象的属性之一(backgroundImgRaw)有一个文件

我必须在数组中循环以上载所有图像并获取文件名,以便保存到db。但是只有最后一个文件被上传,并且所有迭代都会返回名称

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
]);

...
}
只上载最后一张图像。但是create函数的其余部分工作得很好

$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": {},
    ...
    }
]

对于任何有类似问题的人来说,问题在于
$img_name
变量,time()下降到秒,因此所有图像都在同一秒内处理,因此新图像只会不断覆盖以前的图像。我已将该行更新为:


$img_name=Str::slug($request->user['name'],'-')_img_uuz’。str_random(10)。”$img_ext

对于任何有类似问题的人,问题在于
$img_name
变量,时间()下降到秒,因此所有图像都在同一秒内处理,因此新图像只会不断覆盖以前的图像。我已将该行更新为:


$img_name=Str::slug($request->user['name'],'-')_img_uuz’。str_random(10)。”$img_ext

只有最后一个图像被保存,因为您将
create
放在
foreach
循环之外。你必须把它放在循环中

我还注意到您正在使用
time()
生成随机图像名称。这不是一种理想的方法,因为您的代码执行速度太快,所以所有幻灯片都生成相同的
time()
。您可以使用Laravel的
Str::random
函数

以下是我的建议代码:

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
   ]);
}

...
}

仅保存最后一个图像,因为您将
create
放在
foreach
循环之外。你必须把它放在循环中

我还注意到您正在使用
time()
生成随机图像名称。这不是一种理想的方法,因为您的代码执行速度太快,所以所有幻灯片都生成相同的
time()
。您可以使用Laravel的
Str::random
函数

以下是我的建议代码:

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
   ]);
}

...
}

你能告诉我们
$request->slides
返回的值吗?@ThomasVanderVeen我已经更新了问题以显示
$request->slides
returns你能告诉我们
$request->slides
返回的值吗?@ThomasVanderVeen我已经更新了问题,以显示
$request->slides
返回的
stru random(10)
也可以替换为类似于
uniqid()的内容。
str_random(10)
应该很可能是唯一的(大池),但是
uniqid()
应该可以保证这一点。
str_random(10)
也可以用类似
uniqid()
的东西代替
str_random(10)
很可能是唯一的(大池),但是
uniqid()
应该可以保证。谢谢,但这不是解决方案,
create
在foreach循环中。请参阅上面的答案。您确定不希望在foreach循环中创建
create
?您只是在制作问题的最后一张幻灯片。@It_tayo,请查看我的更新答案。我建议您使用
Str::random
生成随机字符串。
create
已经在foreach循环中。是
Str::random
有效。谢谢如果你觉得我的答案有帮助,请把它标为最佳答案。谢谢谢谢,但这不是解决方案,
create
在foreach循环中。请参阅上面的答案。您确定不希望在foreach循环中创建
create
?您只是在制作问题的最后一张幻灯片。@It_tayo,请查看我的更新答案。我建议您使用
Str::random
生成随机字符串。
create
已经在foreach循环中。是
Str::random
有效。谢谢如果你觉得我的答案有帮助,请把它标为最佳答案。谢谢