当我上传多个图像并循环抛出它们时,foreach循环只会获得第一个值

时间:2019-09-21 16:20:07

标签: php laravel

我正在构建一个多图像上传系统,当我开始上传图像并返回$ image var时,它仅显示我的第一个输入。

我已经查看了StackOverflow问题,但发现没有什么可以帮助我

// get the images from the input file 
$images = $request->file('g_images_thumbnails');
// loop throw the result
foreach ($images as $image) {
// return the image value
return $image;
}
// this to open the form
{!! Form::open(['action' => 'AdminBrowsergamesController@store', 'method' => 'POST', 'files' => true]) !!}
// this is the image upload input field
{!! Form::file('g_images_thumbnails[]',['class' => 'form-control', 'multiple', 'accept' => 'image/*']) !!} 
// this is the submit button
{!! Form::submit('Submit',['class' => 'form-control btn btn-info w-50']) !!} 
// this is the close form
{!! Form::close() !!}

它应该循环抛出所有结果并显示它们,但只显示第一个结果。

1 个答案:

答案 0 :(得分:0)

嗨,请尝试以下代码

public function store(Request $request)

    {

        $this->validate($request, [

                'filename' => 'required',
                'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'

        ]);

        if($request->hasfile('g_images_thumbnails'))
         {

            foreach($request->file('g_images_thumbnails') as $image)
            {
                $name=$image->getClientOriginalName();
                $image->move(public_path().'/images/', $name);  
                $data[] = $name;  
            }
         }

         $form= new Form();
         $form->filename=json_encode($data);


        $form->save();

        return back()->with('success', 'Your images has been successfully');
    }

有关Laravel上载多个文件的完整教程,请点击https://appdividend.com/2018/02/05/laravel-multiple-images-upload-tutorial/