Codeigniter3 中的 Dropzone 错误

Dropzone error in Codeigniter3

我在 CI 项目中使用 Dropzone。但是,我无法管理它,我的文件也没有上传。它甚至不将图像名称传递给变量,因为它在插入数据库时​​给出 NULL。

这是我的观点:

<div id="dropzone" class="dropzone"></div>

<script type="text/javascript">
  Dropzone.autoDiscover = false;
  var base_url = "<?php echo base_url();?>";
  $("div#dropzone").dropzone({ 
    url: base_url + 'cars/addpic',
    addRemoveLinks: true,
    uploadMultiple: true,
    paramName: "files",
    acceptedFiles: "image/*",
    dictDefaultMessage: "<span class='mif-file-upload mif-3x'></span> <br>Faykkari sechin gorek"
  });

</script>

这是我的控制器:

public function addpic() {
    $config['upload_path']   = './assets/img/cars/';
    $config['allowed_types'] = 'jpg|png|jpeg|JPG|PNG|JPEG';
    $config['overwrite']     = FALSE;
    $config['remove_spaces'] = TRUE;
    $config['max_size'] = '25000';

    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    if($this->upload->do_upload('files'))
    {
        //for multiple
        $filesCount = count($_FILES['files']['name']);
        for($i = 0; $i < $filesCount; $i++){
            $_FILES['filee']['name'] = $_FILES['files']['name'][$i];
            $_FILES['filee']['type'] = $_FILES['files']['type'][$i];
            $_FILES['filee']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
            $_FILES['filee']['error'] = $_FILES['files']['error'][$i];
            $_FILES['filee']['size'] = $_FILES['files']['size'][$i];



            if(!$this->upload->do_upload('filee')){
                $manyimages = '';
            } else {
                $fileData = $this->upload->data();
                $manyimages[] = $fileData['file_name'];
            }
        } 

        $manymanyimages= implode(',', $manyimages);
    }

然后我将这些数据传递给所有数据都上传到数据库的主函数。

public function create($manymanyimages) {

    //some other stuff here

    $data = array(             
        'photos' => $manymanyimages
    );

    $this->cars_model->add_car($data);
    redirect('');
}

试试这个代码;我认为您的主要问题是嵌套 do_upload() 和 dz 错误处理不当。

    $config['upload_path'] = './assets/img/cars/';
    $config['allowed_types'] = 'jpg|png|jpeg|JPG|PNG|JPEG';
    $config['overwrite'] = FALSE;
    $config['remove_spaces'] = TRUE;
    $config['max_size'] = '25000';
    $this->load->library('upload', $config);
    $manyimages = array();
    $filesCount = count($_FILES['files']['name']);
    for ($i = 0; $i < $filesCount; $i++) {
        $_FILES['filee']['name'] = $_FILES['files']['name'][$i];
        $_FILES['filee']['type'] = $_FILES['files']['type'][$i];
        $_FILES['filee']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
        $_FILES['filee']['error'] = $_FILES['files']['error'][$i];
        $_FILES['filee']['size'] = $_FILES['files']['size'][$i];
        if (!$this->upload->do_upload('filee')) {
            $this->output->set_status_header(500);
            $this->output->set_output(strip_tags($this->upload->display_errors()));
        } else {
            $fileData = $this->upload->data();
            $manyimages[] = $fileData['file_name'];
        }
    }
    $manymanyimages = implode(',', $manyimages);
    echo $manymanyimages;