繁体   English   中英

多dropzone上传1单行mysql

[英]multiple dropzone upload on 1 single line mysql

我正在尝试同时上传多个文件,并在每次上传时在 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>

在我看来,我的 js dropzone 设置很好,但它不能在单行上工作,但它会在数据库中的每个文件中添加一行

是的,正如@ADyson 已经评论的那样,这就是应该的方式; ;-)

PHP 文件从 Dropzone 中的每个文件调用一次!

因此,如果您有 5 个文件,Dropzone 将调用 PHP 文件 5 次,并且每次都交出文件数据。当然,php 文件每次都会创建一个数据库条目。

但是有一个解决方案可以改变这一点: 如何使用 dropzone.js 上传多个文件?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM