Nael.Saeed's avatar

upload multiple images in a form with multiple fileds

Hello there, I am having troubles doing the upload multiple images thing for adding new product form. Dropezone.js doesn't work for me because I need to do the upload with preview and keep the images at the client side until he/she hits the add new product button. I also need to give the user the ability to remove a selected image or images.

This is part one. Part two is in the edit product form. Where I need to know how to show the user all the images he previously added and give the user the ability to remove and add images as needed. also the final commit should be when the user clicks on edit button not before.

how should I do that?

0 likes
6 replies
jlrdw's avatar

Really no different then a single upload just use a foreach Loop. And a search will reveal this has been answered before. asomething like

 public function add() {
        if (isset($_POST['submit'])) {
            $lid = DB::table('recents')->count();
            $k = -1;
            if (empty($lid) || strlen($lid) == 0 || is_null($lid)) {
                $lid = 1;
            }
            $newname = '';
            $destinationPath = ROOTDIR . 'upload/imgrecent/';
            $files = Input::file('ufile');
            //$names = [];
            $arrname = [];
            foreach ($files as $file) {
                $k = $k + 1;
                if (empty($file)) {
                    $arrname[$k] = "";
                } else {
                    $file_name = $file->getClientOriginalName();
                    $file_ext = $file->getClientOriginalExtension();
                    $lid = $lid + $k + 1;
                    $fileInfo = pathinfo($file_name);
                    $filename = $fileInfo['filename'];
                    print_r($filename);
                    $arrname[$k] = $filename . $lid . "." . $file_ext;
                    $file->move($destinationPath, (string) $arrname[$k]);
                }
            }
// more code to do the insert

like, but use laravel request, just quick example

$pic1 = Cln::fixValue((string) $arrname[0]);
            $pic2 = Cln::fixValue((string) $arrname[1]);
            $pic3 = Cln::fixValue((string) $arrname[2]);
            $pic4 = Cln::fixValue((string) $arrname[3]);
            $comments = Cln::fixValue($_POST['comments']);
            if (!isset($error)) {
                $postdata = array(
                    'pic1' => $pic1,
                    'pic2' => $pic2,
                    'pic3' => $pic3,
                    'pic4' => $pic4,
                    'comments' => $comments
                );
                DB::table('recents')->insert($postdata);
            }
        }//end add        

Nael.Saeed's avatar

@jlrdw thanks for your answer. My question is about client side not server side :)

Nael.Saeed's avatar

@MaverickChan as I found out that dropzone.js does the images adding using ajax. I need to add with preview and remove options at the client side until user hit "submit" to the form. Also I need to retrieve those images and show them with delete option in the edit form later on.

Please or to participate in this conversation.