【发布时间】:2021-02-26 13:23:22
【问题描述】:
我按照教程在 HTML 表单中实现了 dropzone。我将表单设置为通过单击按钮来处理文件。单击按钮后,我会看到正在处理的文件,但它们没有被移动到分配的上传目录。我想知道我是否错过了脚本中的某个步骤。我已经尝试了许多通过 stackoverflow 和其他资源找到的建议,但找不到我哪里出错了……但仍在寻找。
我使用 Dropzone 进行了一些文件验证,但想在完成验证并添加 sql 查询以将文件和文本上传到数据库之前到达移动文件的位置。
这是 HTML 表单:
<form action="file_upload.php" method="POST" enctype="multipart/form-data">
<textarea class="form-control border-bottom" name="gallery_text" id="gallery_text" placeholder="Add gallery message..."></textarea>
<div class="dropzone mt-3" id="myDropzone"></div>
<button class="btn btn-primary mt-3" type="submit" name="gallery_submit" id="gallery_submit"> Post Gallery </button>
</form>
...这是脚本:
<script>
Dropzone.options.myDropzone= {
url: "file_upload.php",
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 5,
maxFiles: 5,
maxFilesize: 2,
acceptedFiles: ".jpeg,.jpg,.png,.gif",
addRemoveLinks: true,
dictDuplicateFile: "Duplicate Files Cannot Be Uploaded",
preventDuplicates: true,
init: function() {
dzClosure = this; // Makes sure that 'this' is understood inside the functions below.
// for Dropzone to process the queue (instead of default form behavior):
document.getElementById("gallery_submit").addEventListener("click", function(e) {
// Make sure that the form isn't actually being sent.
e.preventDefault();
e.stopPropagation();
dzClosure.processQueue();
});
//send all the form data along with the files:
this.on("sendingmultiple", function(data, xhr, formData) {
formData.append("gallery_text", jQuery("#gallery_text").val());
});
}
}
...最后上传PHP文件:
<?php require 'inc/config.php';
$folder_name = 'assets/img/posts/';
if (!empty($_FILES['file'])) {
$temp_file = $_FILES['file']['tmp_name'];
$filename = basename($_FILES['file']['name']);
$location = $folder_name . $filename;
move_uploaded_file($temp_file, $location);
}
?>
这里是三个过程的屏幕截图供视觉参考:
我错过了什么?
【问题讨论】: