dropzone.js无法上传多个图片和表单数据

时间:2015-06-08 18:02:41

标签: php dropzone.js

stackoverflow中的第一篇文章..请不要烧我:)

我尝试使用Dropzone.js库执行多个图片上传并使用表单发送其他数据...图像应存储在服务器中,并通过电子邮件发送并提交其他数据。 到目前为止,我可以收到包含附加数据的电子邮件,但没有图像存储在服务器中或通过电子邮件发送。

<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>

PHP文件如下:

<?php

$ds          = DIRECTORY_SEPARATOR;
$storeFolder = 'uploads';
// Get Data 
$name = strip_tags($_POST['data']);
$email = strip_tags($_POST['desc']);
$to_email = "myemailaddress@google.com";
$reply_mail = "myemailaddress@google.com";
$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 = 'from@example.com';
$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';
  }
?>

非常感谢您的帮助

0 个答案:

没有答案