Drag and Drop File Upload using DropzoneJS, PHP & MySQL

https://www.tutorialswebsite.com/demo/drag-and-drop-file-upload-using-dropzone-with-php-mysql/

Drag and drop file upload is a very successful way to make your web application user-friendly. Generally, this feature allows you to drag an element and drop it to a different location on the web page. The drag and drop function can be used for a number of purposes in the web application. Drag & Drop is the best choice if you want to make file upload features more interactive.

DropzoneJS is an open-source JavaScript library that enables a drop-down HTML feature. This means that the user can drag and drop files from the device to this HTML feature.

DropzoneJS does not handle the file upload feature but sends files to the server through AJAX. You need to use PHP to upload files to your server. In this tutorial, we’ll teach you how to integrate drag and drop upload files using Dropzone JS, PHP, and MySQL.

DropzoneJS offers a simple way to combine drag and drop multiple file uploads with a preview of the web application. Dropzone is flexible and does not depend on any other jQuery library.

The following steps will be followed to implement the drag & drop file upload functionality.

  • Droppable element to select multiple files from the computer or device.
  • Preview of the selected files or images.
  • Upload files to the server & Insert files information in the database using PHP & MySQL.

Before getting started, take a look at the file structure of drag & drop file upload with PHP.

Create Database Table

A table is required in the database to store the details of the uploaded file. The following SQL generates a table with some basic fields in the MySQL database.

Create Database Configuration (dbconfig.php)

The dbconfig.php file is used to connect and select the database. Specify the database hostname ($dbHost), username ($dbUsername), password ($dbPassword), and name ($dbName) as per your MySQL database credentials.

Create file (index.php) to Drag and Drop File Upload

The Dropzone JS library will be used to integrate the Drag&Drop feature, so first include the Dropzone JS library and the CSS library.

In the above code, Initialize the Dropzone class and specify the configuration as per your needs.

  • url – File path where the files will be submitted for server-side upload.
  • paramName – The name of the file input field.
  • maxFilesize – Maximum size of the file allowed to upload.
  • maxFiles – Maximum number of files allowed to upload.
  • acceptedFiles – A comma-separated list of allowed mime types.
  • Use the autoProcessQueue option and processQueue method to submit files on button click.
  • Set the autoProcessQueue option to false, that tells Dropzone not to upload the file on the drop.
  • Call processQueue() method to start sending files.

Create (upload.php) to Upload Files to the Server using AJAX

Also Read: Ajax File Upload with Progress Bar using PHP and JQuery

Dropzone sends the all dropped files to the server using ajax file (upload.php) to handle the upload process.

  • Receive posted file data using the PHP $_FILES method.
  • Upload files to the server using move_uploaded_file() function in PHP.
  • Insert the uploaded file info in the database.

Check Live Demo Layout

Drag and Drop File Upload

ALSO READ: Reduce Image Size Before Upload Using PHP

Conclusion:

Drag and drop file upload script provides a user-friendly way to upload images or files to the server. The above code helps you to upload multiple images or files to the server without page refresh using Dropzone and PHP.

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Related posts