Drag and drop multiple file upload using jQuery, Ajax, and PHP

Today we’ll explain to you how to drag and drop multiple file upload using jQuery, Ajax, and PHP.

It is a common functionality widely used in web applications. You may need to upload multiple files to your form with a single uploader when working on a web application.

In this article, we will show you how to upload one or more files without refreshing the page and show thumbnails when files are successfully uploaded using jQuery, Ajax, and PHP.

Let’s create a drag and drop file upload demo that will have the following user interface.

Output - Drag and drop multiple file upload using jQuery, Ajax, and PHP - Clue Mediator
Output – Drag and drop multiple file upload using jQuery, Ajax, and PHP – Clue Mediator

Drag and drop multiple file upload

  1. Create HTML form
  2. Add CSS
  3. Add jQuery script
  4. Write PHP code to upload files
  5. Output

Let’s start with an example to upload multiple files with drag and drop feature. Refer to the following project structure.

File Structure

  • drag-drop-multiple-files
    • uploads
    • db_config.php
    • file_upload.php
    • index.php

1. Create HTML form

First of all, we will create a PHP file named index.php to create a drag and drop file upload HTML UI.

index.php

In the above code, we have added a jQuery library to send Ajax requests to the PHP file file_upload.php for file upload in the directory.

2. Add CSS

Now, we will add the following CSS in the index.php file before the closing head (</head>) tag for the basic UI style.

3. Add jQuery script

Now, we need to add a jQuery script to handle drop, dragover and dragleave events. After dropping files, we will create a formData object and send it to the PHP file file_upload.php via ajax request.

Add the following script in the index.php file before the closing body (</body>) tag.

4. Write PHP code to upload files

To upload files in the directory and save data in MySQL, we need to follow the below steps.

  • Create database

    First, we have to create a uploads table in the database. Run the following SQL query to create a table.

  • Database connection

    We have to create a file for database connection named db_config.php.

    db_config.php

  • PHP code to upload files

    Let’s create a PHP file named file_upload.php to upload files in the uploads directory and save the file name in the database table. Make sure you have created an uploads folder with read and write permissions.

    file_upload.php

5. Output

Run the project and check the output in the browser. You will also see the entry of the image file’s name in the database table.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

Demo & Source Code

Github Repository

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *