【问题标题】:PHP using dropzone inside a form - files not moved the upload directoryPHP 在表单中使用 dropzone - 文件未移动上传目录
【发布时间】: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);
    }
?>

这里是三个过程的屏幕截图供视觉参考:

我错过了什么?

【问题讨论】:

    标签: php forms upload dropzone


    【解决方案1】:

    好吧,我看不出你的代码有什么问题,我只能说,你真的不需要在$_FILES['file']['name'] 上调用basename()basename() 用于从文件路径获取基本名称,例如“C:/User/user/folder/file.txt”。

    所以在下面试试这个

     $filename = $_FILES['file']['name'];
    

    【讨论】:

    • 好电话 - 改变了,但文件仍然没有移动。这是一个令人头疼的问题!
    • 用else语句配合if语句,看看$_FILE变量是否为空,如果是则if语句块中的代码明显跑不起来
    • 我添加了一个 else echo 语句 - 没有输出。就好像它甚至没有到达 file_upload.php
    • 我也是这么认为的,所以也许您可以访问 Dropzone 的文档,看看您是否做得对。我曾经尝试使用 dropzone,但没有成功。我最终使用了一个 jQuery 插件 dmUploader
    • 是的 - 我又回到他们的网站上挖掘了 - 谢谢你的尝试 - 我很感激。
    【解决方案2】:

    事实证明,我需要遍历文件才能移动它们。在 file_upload.php 中,我添加了如下所示的循环。这行得通。

    // Count # of uploaded files in array
    $total = count($_FILES['file']['name']);
    
    
    $filename_arr = [];
    // Loop through each file
    for( $i=0 ; $i < $total ; $i++ ) {
    
        //Get the temp file path
        $tmpFilePath = $_FILES['file']['tmp_name'][$i];
    
        //Make sure we have a file path
        if ($tmpFilePath != ""){
            //Setup our new file path
            $newFilePath = "assets/img/posts/" . $_FILES['file']['name'][$i];
    
            //Upload the file into the temp dir
            if(move_uploaded_file($tmpFilePath, $newFilePath)) {
    
                //Handle other code here
                //$filename_arr[] = $newFilePath;
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-03
      • 2014-11-10
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 2018-09-05
      相关资源
      最近更新 更多