Dropzone上传的文件具有相同的名称

时间:2017-06-14 22:41:04

标签: javascript php codeigniter file-upload dropzone.js

我有一个带有dropzone的普通表单,所有值和文件都上传到服务器但是当我通过打印$ _FILES变量来检查它时,所有上传的文件都有相同的名称&上传最后一个文件的扩展名,但每个文件都有正确的mime-types。因此,当我上传具有不同文件类型的多个文件时,上传始终会获得与最后一个文件具有不同扩展名的文件的错误。

这是我的代码示例(我使用CodeIgniter 3):

HTML

<form class="form form-horizontal dropzone" method="post" id="form-add-product" enctype="multipart/form-data">

<input type="text" class="form-control" name="type" id="type" required>
<input type="number" class="form-control" id="hour_meter" name="hour_meter" required>

<button class="btn btn-flat btn-success" id="btn_add_image" type="button"><i class="fa fa-plus"></i> Add Image</button>

<div id="dropzone-previews" style="min-height: 200px; border: 2px dotted #D2D6DE; padding: 20px">

</form>

JS

var formImage = new Dropzone("#form-add-product", {
    url: baseURL + 'ProductNew/upload_product',
    paramName: 'product-img',
    uploadMultiple: true,
    acceptedFiles: 'image/*',
    maxFiles: 10,
    parallelUploads: 10,
    autoProcessQueue: false,
    previewsContainer: '#dropzone-previews',
    clickable: "#dropzone-previews,#btn_add_image",
    addRemoveLinks: true,
    maxFileSize: 50,
    createImageThumbnails: true,
    resizeHeight: 600,
    dictRemoveFile: "<button class='btn btn-sm btn-flat btn-danger' style='margin-top: 5px'><i class='fa fa-trash'></i></button>",
    init: function() {
        var myForm = this;

        var submitButton = document.getElementById('btn-add-submit');
        submitButton.addEventListener("click", function(e) {
                e.preventDefault();
                e.stopPropagation();

                myForm.processQueue();

        });
});

PHP

$filesCount = count($_FILES['product-img']['name']);
        $temp_files = $_FILES['product-img'];

        $upload_path             = 'assets/images/product/';
        $config['upload_path']   = $upload_path;
        $config['allowed_types'] = 'jpeg|jpg|png|gif|JPG|bmp';
        $config['max_size']      = 1024 * 20;

        if( ! file_exists($upload_path))    
        {
            mkdir('assets/images/product/', 0777, true);
        }

        for($i = 0; $i < $filesCount; $i++)
        {
            $_FILES['product-img']['name']     = $temp_files['name'][$i];
            $_FILES['product-img']['type']     = $temp_files['type'][$i];
            $_FILES['product-img']['tmp_name'] = $temp_files['tmp_name'][$i];
            $_FILES['product-img']['error']    = $temp_files['error'][$i];
            $_FILES['product-img']['size']     = $temp_files['size'][$i];

            $file_name            = 'photos-'.($i+1);
            $config['file_name']  = $file_name;

            $this->upload->initialize($config);

            if($this->upload->do_upload('product-img'))
            {
                $fileData = $this->upload->data();

                $upload_data[] = array(
                    'photo_url'  => $upload_path.$fileData['file_name']
                );
            }
            else
            {
                $upload_data[] = array(
                    'error' => $this->upload->display_errors()
                );
            }
        }

这是截图:

console.log(files)

print_r($_FILES)

给出的错误:

Array
(
    [0] => Array
        (
            [error] => 
The filetype you are attempting to upload is not allowed.


        )

    [1] => Array
        (
            [photo_url] => assets/images/product/rodillo-vibratorio-de-doble-tambor-rd7hes_8567_9096.png
        )

)

我真的不知道问题是什么,我是使用Dropzone的新手,一直在寻找但是没有找到合适的答案。

编辑:我找到了解决方案 我终于找到了解决方案,但我不知道它是否是一个合适的解决方案。我只是在控制器中设置了 $ config [&#39; allowed_type&#39;] =&#39; *&#39; ,上传顺利进行。仍在寻找另一种解决方案来进行服务器端验证。我不会删除这个问题,因为我觉得这对像我这样的初学者很有用。

0 个答案:

没有答案