【发布时间】:2020-01-28 15:39:30
【问题描述】:
我正在尝试上传多张照片!我的代码正在运行,但它只上传一张照片 - 不是所有选定的照片。
我的代码有什么问题?
if(count($_FILES["userfile"]["name"]) == 0) {
$this->session->set_flashdata('success', '?? ????? ?????? ?????');
redirect('accidents/index');
}
else {
// configurations from upload library
$config['upload_path'] = './uploads/images';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048000'; // max size in KB
$config['max_width'] = '20000'; //max resolution width
$config['max_height'] = '20000'; //max resolution height
// load CI libarary called upload
$this->load->library('upload', $config);
for($count = 0; $count < count($_FILES["userfile"]["name"]); $count++) {
// body of if clause will be executed when image uploading is failed
if(!$this->upload->do_upload()) {
$errors = array('error' => $this->upload->display_errors());
// This image is uploaded by deafult if the selected image in not uploaded
$image = 'no_image.png';
}
// body of else clause will be executed when image uploading is succeeded
else {
$data = array('upload_data' => $this->upload->data());
$image = $_FILES['userfile']['name']; //name must be userfile
}
$this->accidents_model->addphoto($image,$last_id);
}
}
模型是:
public function addphoto($photo,$last_id) {
$data = array(
'cp_photo' => $photo,
'ac_id' => $last_id
);
//insert image to the database
$this->db->insert('cars_photos', $data);
}
【问题讨论】:
-
欢迎来到 Stack Overflow!我编辑了您的问题以尝试改善其影响,修改标题,改进代码格式(缩进在 SO 上是一个大问题)并进行了一些小的语法/拼写更正。我希望你同意。
-
这个问题已经回答过很多次了。只需搜索“codeigniter 多张图片上传”,您就会得到很多有效的答案。 Multiple files upload in Codeigniter 的可能重复项