0

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')) }}
  {{ Form::file('img[]', array('class'=>'file', 'multiple'=>true)) }}
  {{ Form::submit('Add images to project', array('class'=>'btn-success btn pull-left')) }}
  {{ 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');
    }

2 Answers 2

3

After some research I found that 'multiple'=>true was the mistake it should be multiple

so the input field will be

{{ Form::file('img[]', array('class'=>'file', 'multiple')) }}
Sign up to request clarification or add additional context in comments.

Comments

0

Try with my code

Controller

 public function img_upload($filename)
    {
        $photo = array('photo' => $filename);
        $destinationPath = 'uploads/multiple'; // upload path
        $original_filename = time().$filename->getClientOriginalName(); // getting image extension
        $extension = $filename->getClientOriginalExtension(); // getting image extension
        //$fileName = rand(11111,99999).'.'.$extension; // renameing image
        $filename->move($destinationPath, $original_filename); 
    }

    public function savemultiimage(Request $request)
    {   
        $files = $request->file('m_name');
        $file_count = count($request->file('m_name'));      
        foreach ($files as $file) {          
                $this->img_upload($file);
                $multi['m_name']=time().$file->getClientOriginalName(); 
                DB::table('multiimage')->insert($multi);
        }
        return redirect('/multifiles/gallery')->with('succ', 'Multiple image successfully inserted!');
    }

View

{!! Form::open(array('url' => 'multifiles/savemultiimage','id'=>'multi_image','files'=>true)) !!}

    <div class="box-body">
        <div class="form-group">
          <label for="exampleInputFile">Multi Images</label>
          <input type="file" id="m_name" name="m_name[]" multiple>          
        </div> 
    </div><!-- /.box-body -->

    <div class="box-footer">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>

  {!! Form::close() !!}

1 Comment

I think this Laravel 5 yes I using Laravel 4.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.