Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Laravel 8 Dropzone Image Upload Example Tutorial

Laravel 8 Dropzone Image Upload Example Tutorial

Posted on September 14, 2020December 15, 2022 By XpertPhp No Comments on Laravel 8 Dropzone Image Upload Example Tutorial

Today, We will explain to you how to upload images using dropzone in laravel 8(Laravel 8 Dropzone Image Upload Example Tutorial). so you can easily upload multiple images using dropzone. Dropzone.js library provides drag and drop facility so it’s a facility using we can easily upload images or files.

We can select, preview, and remove images using the Dropzone.js library. now you can below following the steps.

 

Overview

Step 1: Install Laravel 8

Step 2: Create Routes

Step 3: Create ImageController

Step 4: Create Blade File

Step 5: Run Our Laravel Application

Step 1 : Install Laravel 8

We are going to install laravel 8, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.

1
composer create-project --prefer-dist laravel/laravel laravel8_dropzone_image_upload

Step 2: Create Routes

Add the following route code in the “routes/web.php” file.

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
<?php
use App\Http\Controllers\ImageController;
use Illuminate\Support\Facades\Route;
 
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
 
// Route::get('/', function () {
    // return view('welcome');
// });
 
Route::get('dropzone',[ImageController::class, 'index']);
 
Route::post('dropzone/upload_image',[ImageController::class, 'upload_image'])->name('dropzone.upload_image');
 
Route::get('dropzone/fetch_image',[ImageController::class, 'fetch_image'])->name('dropzone.fetch_image');
 
Route::get('dropzone/delete_image',[ImageController::class, 'delete_image'])->name('dropzone.delete_image');

Step 3: Create ImageuploadController

Here in this step, we will create the ImageController.php file. after then we will create an index, upload_image, fetch_image, and delete_image methods in the ImageController.php file. the first method for view file and if image upload then it will use the second method.

so you can follow the below code.

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
<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class ImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       return view('dropzone');
    }
  
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function upload_image(Request $request)
    {
      
$image = $request->file('file');
 
     $imageName = time() . '.' . $image->extension();
 
     $image->move(public_path('images'), $imageName);
 
     return response()->json(['success' => $imageName]);
  
    }
function fetch_image()
    {
     $images = \File::allFiles(public_path('images'));
     $output = '<div class="row">';
     foreach($images as $image)
     {
      $output .= '<div class="col-md-2">
                <img src="'.asset('images/' . $image->getFilename()).'" class="img-thumbnail" width="150" height="150"/>
                <button type="button" class="btn btn-link remove_image" id="'.$image->getFilename().'">Remove</button>
            </div>';
     }
     $output .= '</div>';
     echo $output;
    }
 
    function delete_image(Request $request)
    {
     if($request->get('name'))
     {
      \File::delete(public_path('images/' . $request->get('name')));
     }
    }
}
?>
See also  laravel eloquent skip and take query example

Step 4: Create Blade File

Finally, We will create a dropzone.blade.php file in the “resources/views/” folder directory and paste the below code.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title> Js</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <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.5.1/dropzone.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/dropzone.js"></script>
</head>
<body>
  <div class="container-fluid">
      <br />
    <h3 align="center"> Laravel 8 Dropzone Image Upload Example Tutorial </h3>
    <br />
        
      <div class="panel panel-default">
        <div class="panel-heading">
          <h3 class="panel-title">Select Image</h3>
        </div>
        <div class="panel-body">
          <form id="dropzoneForm" class="dropzone" action="{{ route('dropzone.upload_image') }}">
            @csrf
          </form>
          <div align="center">
            <button type="button" class="btn btn-info" id="submit-all">Upload</button>
          </div>
        </div>
      </div>
      <br />
      <div class="panel panel-default">
        <div class="panel-heading">
          <h3 class="panel-title">Uploaded Image</h3>
        </div>
        <div class="panel-body" id="uploaded_image">
        </div>
      </div>
    </div>
</body>
</html>
 
<script type="text/javascript">
 
  Dropzone.options.dropzoneForm = {
    autoProcessQueue : false,
    acceptedFiles : ".png,.jpg,.gif,.bmp,.jpeg",
 
    init:function(){
      var submitButton = document.querySelector("#submit-all");
      myDropzone = this;
 
      submitButton.addEventListener('click', function(){
        myDropzone.processQueue();
      });
 
      this.on("complete", function(){
        if(this.getQueuedFiles().length == 0 && this.getUploadingFiles().length == 0)
        {
          var _this = this;
          _this.removeAllFiles();
        }
        load_images();
      });
 
    }
 
  };
 
  load_images();
 
  function load_images()
  {
    $.ajax({
      url:"{{ route('dropzone.fetch_image') }}",
      success:function(data)
      {
        $('#uploaded_image').html(data);
      }
    })
  }
 
  $(document).on('click', '.remove_image', function(){
    var name = $(this).attr('id');
    $.ajax({
      url:"{{ route('dropzone.delete_image') }}",
      data:{name : name},
      success:function(data){
        load_images();
      }
    })
  });
 
</script>

Step 5: Run Our Laravel Application
We can start the server and run this example using the below command.

1
php artisan serve

Now we will run our example using the below Url in the browser.

1
http://127.0.0.1:8000/dropzone

Laravel Tags:Laravel 8, laravel 8 example, laravel 8 image upload, laravel 8 tutorial

Post navigation

Previous Post: Laravel 8 Create Custom Helper File Example
Next Post: Laravel 8 Send Email Example Tutorial

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com