vendredi 4 août 2017

how to iterate on collection of files from form laravel 5

Hello actually I am getting multiple files from view and want to get their names by calling getClientOriginalExtension() then save them into database. Here is code:

View Code:

<form action="" method="post" enctype="multipart/form-data">
                    <input type="hidden" name="_token" value="">
                    @for($i =1; $i <= 30; $i++)
                    <div class="pic clearfix">
                    <div class="col-md-3 mt-5" align="center"><strong>Picture </strong></div>
                    <div class="col-md-3 mt-5 pic-mb" align="center"><input type="file" name="file"   class="" id=""></div>
                    <div class="col-md-3" align="center">
                        <?php
                        $sizes = \App\Models\Admin\ImageSize::all();
                        ?>

                        <select name="size_id" class="form-control">
                            
                            @foreach($sizes as $size)
                                <option value=""></option>
                            @endforeach
                        </select>
                    </div>
                    
                </div>
                @endfor
                    <div class="text-center">
                        <input type="submit" class="btn btn-animated btn-gray-transparent btn-md" data-toggle="modal" value="Send" />
                    </div>
                </form>

Here is controller code:

 // here is i am confused how to get files names from request
        $files = $request->file('file');

        if ($files !== null) {
            foreach ($files as $file) {

                $fileOriginalName = $request->file('file')->getClientOriginalName();
                $fileOriginalMime = $request->file('file')->getClientMimeType();
                $fileBaseName = $request->file('file')->getBasename();
                $fileOriginalExtension = $request->file('file')->getClientOriginalExtension();

                $fileName = str_random() . time() . $fileOriginalName;

                $fileDir = 'uploads/user_data' . '/' . $fileName;

                $file->move('uploads/user_data', $fileName);

                // get image width & height
                $fileSize = getimagesize($fileDir);
                $fileWidth = $fileSize[0];
                $fileHeight = $fileSize[1];

                // save data into database
                $data = new \App\Models\Admin\UserImage();
                $data->filename = $fileName;
                $data->original_filedir = $fileDir;
                $data->original_extension = $fileOriginalExtension;
                $data->dir = 'uploads/user_data';
                $data->original_mime = $fileOriginalMime;
                $data->basename = $fileBaseName;
                $data->original_width = $fileWidth;
                $data->original_height = $fileHeight;
                $data->total_price = $size->price;
                $data->user_id = $user_id;
                $data->size_id = $size_id;
                $data->order_no = rand(100, 10000);
                $data->status = "Pending";
                $data->save();

            }

            return redirect()->route('frontend.data.message', ['id' => $user_id]);

        }

Please guide me how to do that , thanks in advance !



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2voDtEZ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire