dropzone.js Cannot upload multiple image and form data

1.3k views Asked by At

First post in stackoverflow..pls don't burn me :)

I'm trying to use Dropzone.js library to perform a multiple image upload and send additional data with the form...images should be stored in the server and sent via email with the submitted additional data. So far I can get the email with the additional data but no images are stored in the server or sent via email.

<link rel="stylesheet" type="text/css" href="../css/dropzone.css" />
<script type="text/javascript" src="../js/dropzone.js"></script>
</head>
<body>
<div id="contenitore"> 
<form action="process.php" method="post" name="valori" id="my-modulo" class="dropzone">
<input type="text" name="data" placeholder="Some data" autofocus title="Some data" required><br />
<textarea name="desc" placeholder="Inserisci Testo" required></textarea><br />

<div class="dropzone-previews"></div>
<div class="fallback">
<input name="file" type="file" multiple />
</div>

<input type="submit" value="INVIA" id="submit">
</form>
</div>
<script type="text/javascript">
Dropzone.options.myModulo = {

//prevents Dropzone from uploading dropped files immediately
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 25,
maxFiles: 25,
// addRemoveLinks: true,
// previewsContainer: ".dropzone-previews",


// 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
    });
}
};
</script>

The PHP file is the following:

<?php

$ds          = DIRECTORY_SEPARATOR;
$storeFolder = 'uploads';
// Get Data 
$name = strip_tags($_POST['data']);
$email = strip_tags($_POST['desc']);
$to_email = "[email protected]";
$reply_mail = "[email protected]";
$subject = "Messaggio";

// Send Message
$message_body = "I dati inseriti nel messaggio sono:<br />Nome: ".$name."<br />Email: ".$email."<br />Desc:";

require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->From = '[email protected]';
$mail->FromName = 'Amministratore sito';
$mail->addAddress($to_email);
$mail->addReplyTo($reply_email, 'Information');


if(!empty($_FILES)){    
foreach ($_FILES["file"]["error"] as $key => $error)  {
    if ( $error == UPLOAD_ERR_OK ) {  
        $mail->addAttachment($_FILES['file']['tmp_name'][$key]);
        $tempFile = $_FILES['file']['tmp_name'][$key];

        $name = $_FILES["file"]["name"][$key];
        $file_extension = end((explode(".", $name))); # extra () to prevent notice

        $targetPath = FCPATH . $storeFolder . $ds;  //4

        $file_new_name = uniqid(). '.'. $file_extension;

        $targetFile =  $targetPath. $file_new_name  ;  //5

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


  $mail->isHTML(true);                                  // Set email format to HTML

  $mail->Subject = $subject;
  $mail->Body = 'Messaggio di <b>prova</b>' . $message_body;
  $mail->AltBody = 'Questo è il messaggio in formato testo per email senza html';

  if(!$mail->send()) {
     echo 'il messaggio non può essere inviato.';
     echo 'Errore: ' . $mail->ErrorInfo;
  } else {
     echo 'Messaggio inviato';
  }
?>

Thanks a lot for any help

0

There are 0 answers