Upload multiple files in laravel 5

Rakesh Sharma      13 Comments  

Upload multiple files or images in laravel 5:-

Read my previous tutorial upload files in laravel 5 for basics of upload files or images in laravel 5. Now we are going to learn how to upload multiple files or images in laravel 5. sometimes we need to use multiple files or images to upload at once. my previous blog working for upload single file so i am sharing one more blog to upload multiple files or images. let’s check how we can easily upload multiple images in laravel 5.

1. Form :-
Form opening tag must have ‘files’=>true and file field must have name [](with array) and ‘multiple’=>true

{!! Form::open(array('url'=>'apply/multiple_upload','method'=>'POST', 'files'=>true)) !!}
{!! Form::file('images[]', array('multiple'=>true)) !!}

So Upload multiple files in laravel 5 View full code:-

<div class="text-content">
  <div class="span7 offset1">
    <div class="alert-box success">
      <h2>{!! Session::get('success') !!}</h2>
  <div class="secure">Upload form</div>
  {!! Form::open(array('url'=>'apply/multiple_upload','method'=>'POST', 'files'=>true)) !!}
    <div class="control-group">
      <div class="controls">
      {!! Form::file('images[]', array('multiple'=>true)) !!}
	<p class="errors">{!!$errors->first('images')!!}</p>
	<p class="errors">{!! Session::get('error') !!}</p>
{!! Form::submit('Submit', array('class'=>'send-btn')) !!}
{!! Form::close() !!}

Upload multiple files in laravel 5

2. Add below code to your controller function :- I am posting whole code of controller cause laravel 5 have some new changes which need to include most of uses classes manually in our controller class.

<?php namespace App\Http\Controllers;
use Input;
use Validator;
use Redirect;
use Request;
use Session;
class ApplyController extends Controller {
  public function multiple_upload() {
    // getting all of the post data
    $files = Input::file('images');
    // Making counting of uploaded images
    $file_count = count($files);
    // start count how many uploaded
    $uploadcount = 0;
    foreach($files as $file) {
      $rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
      $validator = Validator::make(array('file'=> $file), $rules);
        $destinationPath = 'uploads';
        $filename = $file->getClientOriginalName();
        $upload_success = $file->move($destinationPath, $filename);
        $uploadcount ++;
    if($uploadcount == $file_count){
      Session::flash('success', 'Upload successfully'); 
      return Redirect::to('upload');
    else {
      return Redirect::to('upload')->withInput()->withErrors($validator);

So above you see a simple code about upload multiple files in laravel 5. and now you are done with Upload multiple files in laravel 5.

  • acrord32

    Thanks I was missing the ‘[]’

  • MBguous

    me too, same mistake.. missing []… thanks

  • Hazem Hamaad

    how can i insert the uploaded images into database

    • rakesh sharma

      Just insert filename in db and show your files from saved directory with this name

      • Daniel CaƱizalez

        can you help me to insert the uploaded images into db? i do this into foreach but i have a mistake.

  • http://motarjeman.com Ali Erfani

    I have a form in which the user should select at least one file, and I have 3 file input fields (file1, file2, file3). How can I implement this and validate the mimes? Can you help me?

  • Advanz Virtual Consultant

    can u please just tell me the router file for this.

  • Advanz Virtual Consultant

    can u please just tell me the link or script for how to read the uploaded pdf documents.

  • Manuel Dahmen

    How do I do if I don’t have Form and HTML facades enabled?

  • Test Me

    how do I validate each file that I uploaded using rules in laravel?

  • Djase Dub

    hello it appear to me this

    FatalErrorException in Connection.php line 0:

    Method SymfonyComponentHttpFoundationFileUploadedFile::__toString() must not throw an exception

  • http://www.innorix.com/en/DS Sam Smith

    Thanks for sharing this info. Very useful~

    Sam Smith
    Technology Evangelist and Aspiring Chef
    Large file transfers made easy.

  • http://vk.com/dastiw1 dastiw1