简体   繁体   中英

Only the the last image is uploading while trying to upload multiple image in laravel 5.3

I'm trying to upload multiple image. But the problem is only last image is getting uploaded.Here are my codes

view

<input type="file" id="files" name="file[]" multiple accept="image/*">

controller

if($request->file('file')){
            $files = $request->file;
            foreach ($files as $file){
                $fileName = time().'.'.$file->getClientOriginalExtension();
                $file->move(public_path('/images'), $fileName);
                $Product = Products::select('product_id')->orderBy('product_id','desc')->first();
                $Photo = new Photo();
                $Photo->url = $fileName;
                $Product->Photo()->save($Photo);
            }

How to solve.

There's nothing wrong with your code, Just Select all the files at once So your Input field should display the number of images/files selected, not the last file name: 输入样本

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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