【问题标题】:dropzone.js when count($_FILES["file"]) always get 5当 count($_FILES["file"]) 总是得到 5 时 dropzone.js
【发布时间】:2017-11-08 10:26:32
【问题描述】:

我只是使用这个 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();
});


}
};

我的上传.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 个。

有什么理由吗?

【问题讨论】:

    标签: javascript php dropzone.js


    【解决方案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

    【讨论】:

    • 我试试这个,但它给了我 1,我上传 7 个文件,它唯一的计数 1 使用这个计数($_FILES)
    • 需要对单个请求中的多个文件进行一些额外配置,请参阅编辑
    • 好的,感谢您的帮助,我错过了 uploadMultiple: true
    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 2015-05-22
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多