带有dropzone和参数的Codeigniter上传文件

时间:2018-09-05 10:41:54

标签: codeigniter image-uploading dropzone

它只是不将图像上传到文件夹。到目前为止,它只是添加到数据库中(什么模型是全部),而没有上传。

我希望知道我做错了什么。看下面的代码

HTML

<div class="dropzone infile dz-clickable" id="my-dropzone" name="mainFileUploader">
    <div class="fallback">
        <input name="file" id="file" type="file" multiple />
    </div>
</div>

JS

var myDropzone = new Dropzone("#my-dropzone", {
    url: admin_url+'add_image/material',
    files: this,
    method: "post",
    addRemoveLinks:true,
    acceptedFiles: '.jpg,.jpeg,.JPEG,.JPG,.png,.PNG',
    init: function (data) {
        this.on("sending",function(file, xhr, formData){
            file.token = Math.random().toString(36).substr(2,9);
            formData.append("token",file.token);
            formData.append("type", file.type);
            formData.append("size", file.size);
        });

        this.on("complete", function (file) {
           console.log(file);
        });
        this.on("successmultiple", function(file, response) {
          console.log(file);
        });
        this.on("errormultiple", function(file, response) {
           console.log(file);
        });

            this.on("removedfile",function(file){
                var token = file.token;
                $.ajax({
                    type:"post",
                    data:{token:token},
                    url: admin_url+'remove_image',
                    cache:false,
                    dataType: 'json',
                    success: function(res){

                    }
                });
            });

    },
    dictDefaultMessage: "<div class='drag-icon-cph'><i class='material-icons'>touch_app</i></div><h5>Plaats hier bestanden om te uploaden</h5>",
    dictRemoveFile : "Bestand verwijderen"
});

PHP(Codeigniter)

public function add_image($type, $rel_id = 0){
    if (isset($_FILES['file']['name']) && $_FILES['file']['name'] != '') {
        $path        = getcwd() . '/uploads/';
        $tmpFilePath = $_FILES['file']['tmp_name'];
        if (!empty($tmpFilePath) && $tmpFilePath != '') {
            $path_parts         = pathinfo($_FILES["file"]["name"]);
            $extension          = $path_parts['extension'];
            $extension = strtolower($extension);
            $allowed_extensions = array(
                'jpg',
                'jpeg',
                'png'
            );
            if (!in_array($extension, $allowed_extensions)) {
                set_alert('warning', 'PHP blocked file extention');
                return false;
            }
            $filename    = uniqid(rand(), true).'.'.$extension;
            $newFilePath = $path . '/' . $filename;
            // Upload the file into the upload dir
            if (move_uploaded_file($tmpFilePath, $newFilePath)) {
                $CI =& get_instance();
                $config                   = array();
                $config['image_library']  = 'gd2';
                $config['source_image']   = $newFilePath;
                $config['new_image']      = $filename;
                $config['maintain_ratio'] = true;
                $CI->load->library('image_lib', $config);
                $CI->image_lib->initialize($config);
                $CI->image_lib->resize();
                $CI->image_lib->clear();

                $data = array(
                    'rel_type' => $type,
                    'rel_id' => $rel_id,
                    'attachment_key' => $this->input->post('token'),
                    'file_name' => $filename,
                    'filetype' => $this->input->post('type'),
                    'size' => $this->input->post('size'),
                    'dateadded' => date('Y-m-d H:i:s')
                );
                $id = $this->back_model->add_image($data);
                if($id != ''){
                    array_push($_SESSION['tmp_image'],$id);
                }
                unlink($newFilePath);
            }
        }
    }
}

创建了上载文件夹,为安全起见,我添加了一个空的index.html文件。

.htaccess是标准的Codeigniter

0 个答案:

没有答案