我正在使用 dropzone.js 库,但它不起作用。
在上传脚本中,我计算文件以查看它是否有效:
$file_count = count($_FILES['file']['name']);
echo $file_count;
但它始终只打印 1(我尝试上传 2+)。
HTML:
<form id="propForm" class="" name="" action="upload.php" method="POST" enctype="multipart/form-data">
<div class="option img">
<h4>Imagenes (máximo 6):</h4>
<div id="dropzone" class="dropzone">
// hidden input is appended here
</div>
</div>
// more inputs
<input value="Subir" type="submit" name="submitIT">
</form>
Dropzone选项:
$('div#dropzone').dropzone({
url: 'upload.php',
paramName: "file[]", // The array is initialized here but it's not working
acceptedFiles: 'image/*',
addRemoveLinks: true,
parallelUploads: 6,
maxFilesize: 6,
maxFiles: 6,
autoDiscover: false,
autoProcessQueue: false,
uploadMultiple: true,
hiddenInputContainer: '#dropzone',
init: function () {
thisDropzone = this;
thisDropzone.on("maxfilesexceeded", function(file) { thisDropzone.removeFile(file); });
$("input[type=submit]").click(function(e){
e.preventDefault();
thisDropzone.processQueue();
});
this.on("successmultiple", function(files, response) {
alert('works');
$("form#propForm").submit();
});
}
});
Upload.php 输出:
1 // output for echo $file_count;
Notice: Uninitialized string offset: 0 // $filen = $_FILES['file']['name'][$i]; (inside a for)
编辑:它正在工作,只是文件比表单先上传,所以我收到了 1 和通知。
最佳答案
我的问题是我需要为图像创建一个自定义文件夹(基于表单的数据),所以它不起作用,因为它首先上传文件,然后上传表单。如果有人也需要这个,我发现了这个 jquery plugin :它更基本,它只是在预览区域中转换一个输入类型文件(你必须为每个文件创建或显示一个文件,但你可以使用 jquery 轻松完成)但是 至少它有效 。
上面的代码将首先上传队列中的文件,然后提交表单数据,因此只需确保上传脚本将分别处理两者:
if ($_FILES['file']['error'] == 0) {
// move_uploaded_file (single file)
// or a for($i=0; $i < $file_count; $i++) (multiple files)
}
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
//filter the data and store it in db
}
关于php - 无法使用 dropzone 上传多个文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33695153/