This is the part 2 of 2 in DropzoneJs + PHP series.

In this tutorial, we will show you how to display existing files on the server when using DropzoneJs and PHP. This tutorial is based on How to build a file upload form using DropzoneJs and PHP. Make sure you have read it before proceeding to content in this tutorial.

Table Of Content

  1. Preparation
  2. Modify upload.php
  3. Modify index.php
  4. The end

Content


1. Preparation

In previous tutorial. We have already set up a proper working DropzoneJs upload form. There is no additional file needed for this tutorial. What we need to do is to make some modifications to file below:

  1. upload.php
  2. index.php

Let us get started!

2. Modify upload.php

In previous tutorial. All upload.php does is to store uploaded files to the server directory "uploads". So we need to add a piece of code to retrieve stored files' information (name and size), and return it in JSON format.

Copy the content below to upload.php. Line 16 to 33 is what we added. And we will explain it in details.

As you can see, we added an additional "else" statement, when the HTTP Request does not contain files. This is used to detect already stored files on server for our need. Let us see what they actually do.

  1. Scan "uploads" folder using PHP scandir function. This function returns FALSE if there is any error. And it will return Array of files' names when it is successful.
  2. Loop through the returned value from scandir function and keep them in $result variable. Keep note, we ignore "." and ".." since scandir will always return "." and ".." as valid content referring to current and previous directory.
  3. Output proper header for JSON content and also encode PHP Array to JSON string using json_encode function.
3. Modify index.php

Copy content below to index.php. We will go through modifications individually.

  1. We added Jquery library into our page. This actually not for DropzoneJs directly. We are using Jquery's ajax function $.get only. You will see below
  2. We added an ID element (my-dropzone) to the form. This is needed because we need to pass configuration values to Dropzone. And to do it, we must have an ID reference of it. So that we can configure it by assigning values to Dropzone.options.myDropzone. A lot of people face confusion when configuring Dropzone. To put it in a simple way. Do not take Dropzone as a Jquery plugin, it has its own syntax and you need to follow it.
  3. This starts the main part of modification. What we did here is to pass a function to listen to Dropzone's init event. This event is called when Dropzone is initialized.
  4. Retrieve files details from "upload.php" via ajax.
  5. Create mockFile using values from server. mockFile is simply JavaScript objects with properties of name and size. Then we call Dropzone's addedfile and thumbnail functions explicitly to put existing files to Dropzone upload area and generate its thumbnail.

If you have done it successfully. Now go and upload some images and reload the upload page. Already uploaded files should auto display in Dropzone area.

4.The end

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 or find our mistakes in above tutorial, do leave a comment below to let us know.

Learn DropzoneJS + PHP in advanced level