How to Display Existing Files on Server in Dropzone JS ?

PHP , Laravel 7



Hi Dev,

In this blog, I will teach you how to display existing files or image on server in dropzone js. I will learn to show you existing image on dropzone js in php.

You can simply and easily to upload files or image on server using dropzone js. if you used dropzone js for uploading files and image then you might be need to show existing files or image from database using php mysql.

You will create just two files and one "upload" folder to make it done this example ,so just follow bellow example.

Create Index.php File

<!DOCTYPE html>



<title>How to Display Existing Files on Server in Dropzone JS -</title>

<script src=""></script>

<link href='' type='text/css' rel='stylesheet'>

<script src='' type='text/javascript'></script>

<style type="text/css">

.dz-preview .dz-image img{

width: 100% !important;

height: 100% !important;

object-fit: cover;





<div class="container" >

<h1>How to Display Existing Files on Server in Dropzone JS -</h1>

<div class='content'>

<form action="upload.php" class="dropzone" >




<script type="text/javascript">

Dropzone.autoDiscover = false;


init: function() {

myDropzone = this;


url: 'upload.php',

type: 'post',

data: {request: 'fetch'},

dataType: 'json',

success: function(response){

$.each(response, function(key,value) {

var mockFile = { name:, size: value.size};

myDropzone.emit("addedfile", mockFile);

myDropzone.emit("thumbnail", mockFile, value.path);

myDropzone.emit("complete", mockFile);









Create upload.php File


/* Upload directory*/

$targetDir = "upload/";

$request = "upload";


$request = $_POST['request'];


/* Upload file */

if($request == "upload"){

$msg = "";

if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetDir.$_FILES['file']['name'])) {

$msg = "Successfully uploaded";


$msg = "Error while uploading";


echo $msg;



/* Read files from */

if($request == 'fetch'){

$fileList = [];

$dir = $targetDir;

if (is_dir($dir)){

if ($dh = opendir($dir)){

while (($file = readdir($dh)) !== false){

if($file != '' && $file != '.' && $file != '..'){

$file_path = $targetDir.$file;


$size = filesize($file_path);

$fileList[] = ['name'=>$file, 'size'=>$size, 'path'=>$file_path];







echo json_encode($fileList);



It will help you...

Recommended Posts