DropzoneJS is an open source library that provides drag'n'drop file uploads with image previews. It is a great JavaScript library which actually does not even rely on JQuery. In this tutorial, we are building a multiple file upload form using DropzoneJS, and the backend will be handled by PHP.
Your folder&file structure should look like this after the preparation:
Open file "index.php" and let us create a DropzoneJs form.
Copy the content below to "index.php" and we will go through each line of code individually.
<html>
<head>
<!-- 1 -->
<link href="css/dropzone.css" type="text/css" rel="stylesheet" />
<!-- 2 -->
<script src="dropzone.min.js"></script>>
</head>
<body>
<!-- 3 -->
<form action="upload.php" class="dropzone"></form>
</body>
</html>>
Now go to browser, and navigate to "index.php", you should be able to see a page as below:
However it is not quite done yet, we will still have to code for the file uploading process.
Now you have come to Last part of the tutorial. In this section, we will store files sent from DropzoneJS to the "uploads" folder.
Open "upload.php" and copy the content below:
<?php
$ds = DIRECTORY_SEPARATOR; //1
$storeFolder = 'uploads'; //2
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name']; //3
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; //4
$targetFile = $targetPath. $_FILES['file']['name']; //5
move_uploaded_file($tempFile,$targetFile); //6
}
?>
Now go to browser, and try DropzoneJS upload form again, it should work nicely:
If you encounter any uploading issues related to PHP or server, you can check out this page, where we discuss some common Move_uploaded_file problems.
If you encounter any DropzoneJS issues, please check out its official page.
Hopefully this simple tutorial helped you with your development. If you like our post, please follow us on Twitter and help spread the word. We need your support to continue. If you have questions, do leave a comment below to let us know.