dropzone.js当count($ _ FILES [" file"])总是得到5

时间:2017-11-08 10:26:32

标签: javascript php dropzone.js

我只是使用这个dropzone.js并发现这个错误或我做错了什么? 文档没有任何演示。

我已经搜索了这个,但没有看到任何有关此问题的问题/问题

我的HTML代码:

<button id="submit-all" class="btn btn-primary">Submit all files</button> // for upload all
<form action="upload.php" class="dropzone" id="my-dropzone">
  <label>Username:<input type="text" name="uname"/> </label> // only for test
  <label>Password:<input type="text" name="pass"/> </label> // only for test
  <div id="dropzonePreview"></div> 
</form>

表示js:

Dropzone.options.myDropzone = {

// Prevents Dropzone from uploading dropped files immediately
autoProcessQueue: false,
parallelUploads:100,

init: function() {
var submitButton = document.querySelector("#submit-all")
myDropzone = this; // closure

submitButton.addEventListener("click", function() {
myDropzone.processQueue(); // Tell Dropzone to process all queued files.
});

// You might want to show the submit button only when 
// files are dropped here:
this.on("addedfile", function(file) {

// Create the remove button
var removeButton = Dropzone.createElement("<button class='removebutton'>Remove file</button>");


// Capture the Dropzone instance as closure.
var _this = this;

// Listen to the click event
removeButton.addEventListener("click", function(e) {
// Make sure the button click doesn't submit the form:
e.preventDefault();
e.stopPropagation();

// Remove the file preview.
_this.removeFile(file);
// If you want to the delete the file on the server as well,
// you can do the AJAX request here.
});

// Add the button to the file preview element.
file.previewElement.appendChild(removeButton);
});

this.on("success", function(file, responseText) {
// Handle the responseText here. For example, add the text to the preview element:
$(".removebutton").hide();
});


}
};

我的upload.php:

session_start();
$_SESSION["count"] = count($_FILES["file"]); // this always print 5
for ($i=0; $i < count($_FILES["file"]) ; $i++) { 

$target_dir = "gambar/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
// uplaod image
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
$uname=$_POST['uname'];
$_SESSION["uname"] = $uname;
$pass=$_POST['pass'];
$_SESSION["pass"] = $pass;
}

当我打印会话进行计数时,即使我上传了7个文件或2个文件,它总是给我5个。

任何原因?

1 个答案:

答案 0 :(得分:1)

因为通过执行count($_FILES["the_first_file_input_name"]),您不计算上传的数字文件,而是计算文件本身的属性数

看看PHP文档,$ _FILES数组采用关联数组的形式,每个文件项本身就是一个由5个标准成员组成的关联数组:

Array => (
          [file1] => Array (
                            [name] => SomeFile.jpg
                            [type] => image/jpeg
                            [tmp_name] => /tmp/php/tmp_name
                            [error] => UPLOAD_ERR_OK
                            [size] => 12345
                           )
          [file2] => Array (
                            [name] => SomeOtherFile.jpg
                            [type] => image/jpeg
                            [tmp_name] => /tmp/php/other_tmp_name
                            [error] => UPLOAD_ERR_OK
                            [size] => 123456
                           )
         )

您需要计算文件数组count($_FILES)

修改

看起来你已经配置了drozonejs来使用并行的单个请求处理排队的文件,每个文件上传一个,所以这个计数现在是1,但执行n次文件。可以选择在单个上传请求中允许多个文件,而不是uploadMultiple: true http://www.dropzonejs.com/#config-uploadMultiple