In this tutorial you will learn about the Laravel 5.8 Dropzone Multiple Image Upload with Remove Link and its application with practical example.
Laravel 5.8 Dropzone Multiple Image Upload with Remove Link
In this Laravel 5.8 Dropzone Multiple Image Upload with Remove Link example, I will show you how to upload files with drag and drop interface using dropzone.js in your Laravel 5 applications. Dropzone is an open source light weight JavaScript library that provides drag and drop features to upload images with preview images. In this example before uploading the image we will display preview of the image along with a remove image link and then save it into directory. In order to use dropzone plugin we need to include the Dropzone javascript and CSS files in your laravel blade file. This tutorial we learn to :-
- Uploading multiple images with dropzone
- Saving images with unique file names to database
- Removing images directly from dropzone preview box
Install Laravel 5.8
First of all we need to create a fresh laravel project, download and install Laravel 5.8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel DropzoneImageUpload |
Configure Database In .env file
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
.env
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=DropzoneImageUpload DB_USERNAME=root DB_PASSWORD= |
Generate Migration
Now, we have to define table schema for photos table. Open terminal and let’s run the following command to generate a migration file to create photos table in our database.
1 |
php artisan make:migration create_photos_table |
Once this command is executed you will find a migration file created under “database/migrations”. lets open migration file and put following code in it –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePhotosTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('photos', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('photo_name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('photos'); } } |
Run Migration
Now, run following command to migrate database schema.
1 |
php artisan migrate |
After, the migration executed successfully the photos table will be created in database along with migrations, password_resets and users table.
Create Model
Next, we need to create a model called Photo using below command.
1 |
php artisan make:model Photo |
Once, the above command is executed it will create a model file Photo.php in app directory.
Create Controller
Now, create a controller to upload image. Create a controller named ImageUploadController using command given below –
1 |
php artisan make:controller dropzone_image_upload/ImageUploadController |
Once the above command executed, it will create a controller file ImageUploadController.php in app/Http/Controllers/dropzone_image_upload/ directory. Open the ImageUploadController.php file and put the following code in it.
app/Http/Controllers/dropzone_image_upload/ImageUploadController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php namespace App\Http\Controllers\dropzone_image_upload; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Validator,Redirect,Response,File; use App\Photo; class ImageUploadController extends Controller { // public function index() { return view('dropzone_image_upload.index'); } public function store(Request $request) { $image = $request->file('file'); $profileImage = $image->getClientOriginalName(); // Define upload path $destinationPath = public_path('/profile_images/'); // upload path $image->move($destinationPath,$profileImage); // Save In Database $imagemodel= new Photo(); $imagemodel->photo_name="$profileImage"; $imagemodel->save(); return response()->json(['success'=>$profileImage]); } public function destroy(Request $request) { $filename = $request->get('filename'); Photo::where('photo_name',$filename)->delete(); $path=public_path('/profile_images/').$filename; if (file_exists($path)) { unlink($path); } return $filename; } } |
Here In the controller, we have following methods –
index() :- It displays Drozone Multiple Image Upload Form
store() :- To Upload using Dropzone and Save Multiple Image in database.
destroy() :- To delete files from directory and database.
Note:- Before uploading any file make sure you have created following two directory in the public folder called profile_images.
Create Blade / View Files
In this step, we will create view/blade file to display Image Upload Form using Dropzone. Lets create a blade file “index.blade.php” in “resources/views/dropzone_image_upload/” directory.
Include Dropzone Javascript and CSS files
In order to use dropzone plugin we need to include following Dropzone javascript and CSS files in your laravel blade file.
Dropzone CSS :-
In this file we will be adding dropzone.min.css along with the our bootstrap.min.css as following –
Dropzone Javascript :-
Here we will add dropzone.js file along with the jquery.js as following –
1 2 |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/dropzone.js"></script> |
Configure Dropzone Plugin with Remove Image Link
Next, we will provide initial configurations for Dropzone plugin.We setup follwoing dropzone options –
maxFilesize:- Maximum Image size that can be uploaded.
renameFile:- It is used to rename the file before uploading.
addRemoveLinks:- It is set to true to display Remove button to remove uploaded file.
removedfile:- It is a callback function invoked when image is removed from dropzone.
Now, our blade file will look something like this.
resources/views/dropzone_image_upload/index.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
<!DOCTYPE html> <html> <head> <title>Laravel Dropzone Multiple Images Uploading - W3Adda</title> <meta name="_token" content="{{csrf_token()}}" /> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/min/dropzone.min.css"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/dropzone.js"></script> </head> <body> <div class="container"> <h3>Laravel Dropzone Multiple Images Uploading - W3Adda</h3> <form method="post" action="{{url('dropzone-image-upload')}}" enctype="multipart/form-data" class="dropzone" id="dropzone"> @csrf </form> <script type="text/javascript"> Dropzone.options.dropzone = { maxFilesize: 12, renameFile: function(file) { var dt = new Date(); var time = dt.getTime(); return time+file.name; }, acceptedFiles: ".jpeg,.jpg,.png,.gif", timeout: 5000, addRemoveLinks: true, removedfile: function(file) { var name = file.upload.filename; $.ajax({ headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }, type: 'POST', url: '{{ url("dropzone-image-delete") }}', data: {filename: name}, success: function (data){ console.log("File deleted successfully!!"); }, error: function(e) { console.log(e); }}); var fileRef; return (fileRef = file.previewElement) != null ? fileRef.parentNode.removeChild(file.previewElement) : void 0; }, success: function(file, response) { console.log(response); }, error: function(file, response) { return false; } }; </script> </body> </html> |
Create Routes
After this, we need to add following dropzone image upload routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.
routes/web.php
1 2 3 |
Route::get('dropzone-image-upload', 'dropzone_image_upload\ImageUploadController@index'); Route::post('dropzone-image-upload', 'dropzone_image_upload\ImageUploadController@store'); Route::post('dropzone-image-delete', 'dropzone_image_upload\ImageUploadController@destroy'); |
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
http://localhost:8000/dropzone-image-upload
Output:-
After Image Upload Screen Output:-
Select and Drop Multiple Image files to upload using dropzone.