我在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);
}
然后我将此数据传递给main函数,其中所有数据都上传到数据库。
public function create($manymanyimages) {
//some other stuff here
$data = array(
'photos' => $manymanyimages
);
$this->cars_model->add_car($data);
redirect('');
}
答案 0 :(得分:0)
试试这段代码;我认为你的主要问题是嵌套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;