Laravel 5 - Multiple Images Uploading with drag and drop using dropzone.js example

Laravel 5 - Multiple Images Uploading with drag and drop using dropzone.js example

In this PHP Laravel Tutorial, I will tell you how to upload multiple images with preview using dropzone.js with example.

There are many plugins available to upload multiple images with preview. Dropzone provides the easiest way to upload multiple files with best user interface.

Dropzone is an open source and light weight JavaScript library that provides the drag and drop features to upload images with preview images.

Dropzone does not handle the server side script so you will have to write your own code to save file in a specific path.

There are filter options with dropzone such as you can set validation for max upload or apply restriction to upload only image file or have any other file extension.

For this example, you will need the dropzone library :

  1. dropzone.css
  2. dropzone.js
Step 1: Routes

In this first step, We will add two following routes :

routes/web.php
  1. Route::get('dropzoneFile','HomeController@dropzoneFile') ;
  2. Route::post('dropzoneUploadFile',array('as'=>'dropzone.uploadfile','uses'=>'HomeController@dropzoneUploadFile')) ;

First route is used to render view file and second routes is used to save file uploaded with dropzone plugin.

Step 2: HomeController with two Method

In this step, We will create HomeController file in following path app/Http/Controllers/ and define two method in following way :

app/Http/Controllers/HomeController.php
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests;
  4. use Illuminate\Http\Request;
  5. class HomeController extends Controller
  6. {
  7. public function dropzoneFile(){
  8. return view('dropzone_file_upload');
  9. }
  10. public function dropzoneUploadFile(Request $request){
  11. $imageName = time().'.'.$request->file->getClientOriginalExtension();
  12. $request->file->move(public_path('images'), $imageName);
  13. return response()->json(['success'=>$imageName]);
  14. }
  15. }

Now create a folder "images" in public directory to store all files.

Step 3: Add Blade File

In this last step, We will create a view file "dropzone_file_upload.blade.php" in following path resources/views/.

resources/views/dropzone_file_upload.blade.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <title>Laravel: upload multiple file using drag and drop feature</title>
  8. <link href="http://demo.expertphp.in/css/dropzone.css" rel="stylesheet">
  9. <script src="http://demo.expertphp.in/js/dropzone.js"></script>
  10. </head>
  11. <body>
  12. <h3>Laravel 5.4 : Upload multiple file using dropzone</h3>
  13. {!! Form::open([ 'route' => [ 'dropzone.uploadfile' ], 'files' => true, 'class' => 'dropzone','id'=>"image-upload"]) !!}
  14. {!! Form::close() !!}
  15. </body>
  16. </html>

You can set the validation option with dropzone in following way :

  1. <script type="text/javascript">
  2. Dropzone.options.imageUpload = {
  3. maxFilesize : 1,
  4. acceptedFiles: ".jpeg,.jpg,.png,.gif"
  5. };
  6. </script>

Phone: (+91) 8800417876
Noida, 201301