如何解决在 1 个单行 mysql 上上传多个 dropzone
我正在尝试同时上传多个文件,并在每次上传时在 1 个单独的 mysql 行中插入文件路径,但这会为每个文件添加一行。
在我看来,我的 js dropzone 设置很好,但它在一行上不起作用,但它为数据库中的每个文件添加了一行
感谢您的帮助。 (对不起,我是法国人)
我的js:
/*DROPZONE*/
Dropzone.autoDiscover = false;
$(document).ready(function() {
$("#divDropzone").dropzone({
url: 'image-add.php',parallelUploads: 100,paramName: 'file',maxFiles: 3,maxFilesize: 1.5,dictDefaultMessage: 'Drop files here to upload...',uploadMultiple: true,// processingmultiple: true,// resizeWidth: 150,// resizeHeight: 150,// resizeMethod: "contain",acceptedFiles: 'image/png,image/gif,image/jpg,image/jpeg,image/bmp,image/x-icon,image/svg+xml,image/tiff,application/pdf,application/msword,application/vnd.openxmlformats-officewordprocessingml.document,application/vnd.oasis.openspreadsheet,application/vnd.oasis.opentext,application/vnd.ms-powerpoint,application/vnd.ms-excel,application/vnd.openxmlformats-officespreadsheetml.sheet',autoProcessQueue: false,addRemoveLinks: true,dictRemoveFile: 'Remove',dictMaxFilesExceeded: 'You can only upload 3 files please delete some !',dictFileTooBig: 'File too large,up to 1 MB per file !',dictInvalidFileType: 'Invalid file only images and Microsoft Office Documents are accepted !',init: function() {
myDropzone = this;
$("#uploadBtn").click(function(e) {
e.preventDefault();
e.stopPropagation();
myDropzone.processQueue();
});
}
});
});
我的 php 和我的 html:
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
// Database
include 'db.php';
if (! empty($_FILES)) {
$uploadsDir = "uploads/";
$allowedFileType = array('jpg','png','jpeg','pdf','jfif','psd','doc','docx','xls','xlsx');
$uniqueName = md5(uniqid(rand(),true));
$separator = '---';
// Validate if files exist
if (!empty(array_filter($_FILES['file']['name']))) {
// Loop through file items
foreach($_FILES['file']['name'] as $id=>$val){
// Get files upload path
$fileName = $_FILES['file']['name'][$id];
$tempFile = $_FILES['file']['tmp_name'][$id];
$targetFilePath = $uploadsDir . $uniqueName . $separator . $fileName;
$fileType = strtolower(pathinfo($targetFilePath,PATHINFO_EXTENSION));
$uploadDate = date('Y-m-d H:i:s');
if(in_array($fileType,$allowedFileType)){
if(move_uploaded_file($tempFile,$targetFilePath)){
$sqlVal = "('".$targetFilePath."','".$uploadDate."')";
}
}
// Add into MySQL database
if(!empty($sqlVal)) {
$insert = $conn->query("INSERT INTO infos_devis (image_path,date_time) VALUES $sqlVal");
}
}
}
}
?>
<html>
<head>
<title>Add New Image</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<link rel="stylesheet" type="text/css" href="dropzone/dropzone.css" />
</head>
<body>
<div class="container">
<form name="frmImage" action="image-add.php?action=save" class="dropzone" id="divDropzone">
</form>
<button class="btn btn-primary text-white" id="uploadBtn">Upload</button>
<div class="btn-menu">
<a href="index.php" class="link">Back to List</a>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="dropzone/dropzone.js"></script>
<script src="script.js"></script>
</body>
</html>
解决方法
在我看来,我的 js dropzone 设置很好,但它在一行上不起作用,但它为数据库中的每个文件添加了一行
是的,因为@ADyson 已经评论过这是应该的! ;-)
PHP 文件在 Dropzone 的每个文件中调用一次!
所以如果你有 5 个文件,Dropzone 会调用 PHP 文件 5 次,每次都交出文件数据.. 当然,php 文件每次都会创建一个数据库条目。
但是有一个解决方案可以改变这一点: How to upload multiple files using dropzone.js?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。