How do I dropzoneJS view files that have already been uploaded? I followed the steps in the documentation and are uploading normally, but do not display the already uploaded files.
My form HTML code:
<form action="upload.php" class="dropzone" id="my-dropzone"></form>
Script:
<script type="text/javascript">
$(document).ready(function() {
Dropzone.autoDiscover = false;
$("#my-dropzone").dropzone({
//url: "/file/post",
addRemoveLinks : true,
maxFilesize: 0.5,
dictResponseError: 'Erro ao fazer o upload !'
});
})
</script>
<script type="text/javascript">
Dropzone.options.myDropzone = {
init: function() {
thisDropzone = this;
$.get('upload.php', function(data) {
$.each(data, function(key,value){
var mockFile = { name: value.name, size: value.size };
thisDropzone.options.addedfile.call(thisDropzone, mockFile);
thisDropzone.options.thumbnail.call(thisDropzone, mockFile, "uploads/"+value.name);
});
});
}
};
</script>
Code in upload.php:
<?php
$ds = DIRECTORY_SEPARATOR;
$storeFolder = 'uploads';
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
$targetFile = $targetPath. $_FILES['file']['name'];
move_uploaded_file($tempFile,$targetFile);
} else {
$result = array();
$files = scandir($storeFolder);
if ( false!==$files ) {
foreach ( $files as $file ) {
if ( '.'!=$file && '..'!=$file) {
$obj['name'] = $file;
$obj['size'] = filesize($storeFolder.$ds.$file);
$result[] = $obj;
}
}
}
header('Content-type: text/json');
header('Content-type: application/json');
echo json_encode($result);
}
?>