johnef_sh's avatar

Upload multiple image but First image only uploaded

I am using Laravel 4.2 and I have this form to upload multiple images

the problem when I submit the form it returns to the view page and the first image only uploaded. can anyone please review my code and correct my mistake

{{ Form::open(array('url'=>'doAddProjectImage', 'files'=>'true', 'method'=>'PUT', 'class'=>'margin-top-30 width-100per pull-left')) }}
                    <h4 class=" header-01 margin-top-15"><strong><i class="fa fa-edit"></i> Product Images</strong>
                    </h4>

                    <div class="form-group">
                        {{ Form::label('img', 'Image 1', array('class'=>'col-sm-2 row control-label')) }}
                        <div class="col-sm-10">
                            {{ Form::file('img[]', array('class'=>'file', 'multiple'=>true)) }}
                        </div>
                    </div>

                    <div class=" form-group">
                        <div class="col-sm-2 col-md-3 col-lg-2 pull-left"></div>
                        <div class="col-sm-10 col-md-9  col-lg-10">

                            {{ Form::submit('Add images to project', array('class'=>'btn-success btn pull-left')) }}
                        </div>
                    </div>

                    {{ Form::hidden('pid', Session::get('insId')) }}
                    {{ Form::close() }}

and this is my controller

public function doAddProjectImage()
    {
        $proId = Input::get('pid');

        $projectImages = new ProjectsImages();
        $files = Input::file('img');
        foreach($files as $file) {
            $destination_path = 'images/projects/';
            $filename = str_random(6) . '_' . $file->getClientOriginalName();
            $file->move($destination_path, $filename);

            $projectImages->image = $filename;
            $projectImages->image_id = $proId;
            $projectImages->save();
        }

        return Redirect::to('admin/view-project')->with('message', 'Project deleted successfully');
    }
0 likes
0 replies

Please or to participate in this conversation.