Question

I am creating a drop zone form using dropzone.js. I firstly set the form up to automatically upload the file this worked fine, but I adapted the form to work only when the user submitted the data, i added file called custom_dropzone.js and the form appeared to work but the files never got uploaded to the folder.

HTML CODE (index.php)

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<head>   
<link href="css/dropzone.css" type="text/css" rel="stylesheet" />
<script src="dropzone.min.js"></script> 
<script src="custom_dropzone.js"></script>  

 <!-- Now setup your input fields -->
 <input type="email" name="username" />
 <input type="password" name="password" />

 <button type="submit">Submit data and files!</button>
</form>
</body>
</html> 

upload.php

<?php
$ds          = DIRECTORY_SEPARATOR;  //1

$storeFolder = '../upload_test/uploads';   //2

if (!empty($_FILES)) {


 $tempFile = $_FILES['file']['tmp_name'];          //3             

 $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  //4

 $targetFile =  $targetPath. $_FILES['file']['name'];  //5


$allowed =  array('gif','png' ,'jpg');
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
echo 'error';
}

move_uploaded_file($tempFile,$targetFile); //6

}
?>  

NEW JS custom.dropzone.js which seems to break the upload.php function

Dropzone.options.myAwesomeDropzone = { // The camelized version of the ID of the form       element

  // The configuration we've talked about above
  autoProcessQueue: false,
  uploadMultiple: true,
  parallelUploads: 3,
  maxFiles: 3,
  previewsContainer: ".dropzone-previews",

  accept: function(file, done) {
    console.log("uploaded");
    done();
   },

  init: function() {
  this.on("maxfilesexceeded", function(file){
    alert("No more files please!");
  });
},

// The setting up of the dropzone
init: function() {
  var myDropzone = this;

  // First change the button to actually tell Dropzone to process the queue.
  this.element.querySelector("button[type=submit]").addEventListener("click",   function(e) {
    // Make sure that the form isn't actually being sent.
    e.preventDefault();
    e.stopPropagation();
    myDropzone.processQueue();
   });

   // Listen to the sendingmultiple event. In this case, it's the sendingmultiple event instead
   // of the sending event because uploadMultiple is set to true.
   this.on("sendingmultiple", function() {
  // Gets triggered when the form is actually being sent.
  // Hide the success button or the complete form.
   });
   this.on("successmultiple", function(files, response) {
  // Gets triggered when the files have successfully been sent.
  // Redirect user or notify of success.
  });
  this.on("errormultiple", function(files, response) {
  // Gets triggered when there was an error sending the files.
  // Maybe show form again, and notify user of error
  });
 }

}

thanks for a lll your help. I just need the file to be uploaded, every thing else seems to work fine

Was it helpful?

Solution 2

With dropzone it seems that you do not need method='post' enctype='multipart/form-data' like what was mentioned by Shankar … but thank you

I resolved this by commenting out the below line of js from custom_dropzone.js
//uploadMultiple: true,

OTHER TIPS

You forgot to add enctype='multipart/form-data' to your <form> as an attribute and the method type as POST.

Add like this..

 <form id="my-awesome-dropzone" method='post' class="dropzone" action="upload.php" enctype='multipart/form-data'>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top