【发布时间】:2021-04-10 12:39:12
【问题描述】:
我正在尝试同时上传多个文件,并在每次上传时将文件路径插入 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/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>
【问题讨论】:
-
您到底为什么希望它们都在数据库的一行中?这与好的设计相反。在学习关系数据库设计时,你(应该)学到的第一条规则之一是,你永远不要在一行的同一列中存储多个值。另一个是你很少应该有多个列在一个表中存储相同类型数据的实例——这两者都表明数据是非规范化的
标签: javascript php file-upload upload dropzone