Drag and Drop File Upload using Dropzone JS in PHP

[ad_1]

Drag and drop upload file in PHP; In this tutorial, we will learn how to create drag and drop file upload files using dropzone js in PHP. And upload multiple files in PHP.

Dropzone. js is ‘a light weight JavaScript library that turns an HTML element into a “dropzone“‘. Users can drag and drop a file onto an area of the page, uploading to a server. … js code to our project and create a ‘drop area’ for file uploading.

Note that, Always prefer to use dropzone js for drag and drop multiple file uploading into directory and database. And you will learn step by step how to integrate dropzone js in PHP with html. And upload multiple file with drag and drop feature.

Drag and Drop File Upload in PHP MySQL using Dropzone JS

Follow following the below steps and upload file using jQuery dropzone js in PHP:

  • Step 1 – Create index.php
  • Step 2 – Create upload.php
  • Step 3 – Create a Directory

Step 1 – Create index.php

First of all, create an index.php file and update the below HTML code into index.php file.

<!DOCTYPE html>
<html>
<head>
  <title>PHP Dropzone File Upload Example Tutorial</title>
  <!-- Bootstrap Css -->
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">

  <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
  
  <link href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/min/dropzone.min.css" rel="stylesheet">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.2.0/min/dropzone.min.js"></script>
</head>
<body>
  
<div class="container">
  <div class="row">
    <div class="col-md-12">
      <h2>PHP Dropzone File Upload on Button Click Example</h2>
      <form action="upload.php" enctype="multipart/form-data" class="dropzone" id="image-upload">
        <div>
          <h3>Upload Multiple Image By Click On Box</h3>
        </div>
      </form>
      <button id="uploadFile">Upload Files</button>
    </div>
  </div>
</div>
  
<script type="text/javascript">
  
    Dropzone.autoDiscover = false;
  
    var myDropzone = new Dropzone(".dropzone", { 
       autoProcessQueue: false,
       maxFilesize: 1,
       acceptedFiles: ".jpeg,.jpg,.png,.gif"
    });
  
    $('#uploadFile').click(function(){
       myDropzone.processQueue();
    });
      
</script>
  
</body>
</html>

This HTML code shows the image upload form, so using this form you can upload the images on the DB table and project folder.

Step 2 – Create upload.php

In this step, create a new file name upload.php file and update the below code into your upload.php file.

<?php


$uploadDir = 'uploads';


if (!empty($_FILES)) {
 $tmpFile = $_FILES['file']['tmp_name'];
 $filename = $uploadDir.'/'.time().'-'. $_FILES['file']['name'];
 move_uploaded_file($tmpFile,$filename);
}


?>

This PHP code will upload file into project directory.

Step 3 – Create Directory

In this step, we need to create a directory into your project directory, which named uploads.

Conclusion

In this tutorial, we have learned how to upload files using dropzone js in PHP without refresh the whole web page.

Recommended PHP Tutorials

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00