douxitao8170 2015-11-13 14:37
浏览 91
已采纳

无法使用dropzone上传多个文件

I'm using the dropzone.js library but it's not working.

In the upload script I count the files to see if it works:

$file_count = count($_FILES['file']['name']);
echo $file_count;

But it only prints 1 all the time (I tried uploading 2+).

HTML:

<form id="propForm" class="" name="" action="upload.php" method="POST" enctype="multipart/form-data">
    <div class="option img">
        <h4>Imagenes (m&#225;ximo 6):</h4>
        <div id="dropzone" class="dropzone">
        // hidden input is appended here
        </div>
    </div>
    // more inputs
    <input value="Subir" type="submit" name="submitIT">
</form>

Dropzone options:

$('div#dropzone').dropzone({
    url: 'upload.php',
    paramName: "file[]", // The array is initialized here but it's not working
    acceptedFiles: 'image/*',
    addRemoveLinks: true,
    parallelUploads: 6,
    maxFilesize: 6,
    maxFiles: 6,
    autoDiscover: false,
    autoProcessQueue: false,
    uploadMultiple: true,
    hiddenInputContainer: '#dropzone',

    init: function () {
        thisDropzone = this;
        thisDropzone.on("maxfilesexceeded", function(file) { thisDropzone.removeFile(file); });
        $("input[type=submit]").click(function(e){ 
            e.preventDefault();
            thisDropzone.processQueue();
        });
        this.on("successmultiple", function(files, response) {
            alert('works');
            $("form#propForm").submit();
        });
    }
});

Upload.php output:

1  // output for echo $file_count;
Notice: Uninitialized string offset: 0 // $filen = $_FILES['file']['name'][$i]; (inside a for)

EDIT: It was working, just that the files are uploaded first than the form, so I was getting 1 and the notice.

  • 写回答

1条回答 默认 最新

  • douqinlu4217 2015-11-15 04:35
    关注

    My problem is that I need to create a custom folder for the images (based on the data of the form) so it won't work because it first upload the files and then the form. If someone needs this too, I found this jquery plugin: it's more basic, it just converts an input type file in a preview area (you'll have to create or show one for each file but you can do this easily with jquery) but at least it works.

    The above code will just work to first upload the files in the queue and then submit the form data so just make sure the upload script will handle both separately:

    if ($_FILES['file']['error'] == 0) {
    // move_uploaded_file (single file)
    // or a for($i=0; $i < $file_count; $i++) (multiple files)
    }
    
    if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    //filter the data and store it in db
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题