for循环不会上传Codeigniter3中的所有图像

时间:2018-04-16 11:40:14

标签: php codeigniter

我有网站,我一次上传几张照片。在某些时刻,我注意到,它没有上传所有必需的照片。然后我将照片从1.jpg重命名为14.jpg,以便查看我的代码上传了什么。它仅上传奇数(2.jpg,4.jpg,6.jpg ... 14.jpg)。问题出在哪里?

public function addpic() {
            $manymanyimages = '';
            $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);
            $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'];
                }
            }

            $last_id = $this->db->select('id')->order_by('id',"desc")->limit(1)->get('cars')->row()->id + 1;

            $data = array(
                'img_name' => $manyimages,
                'iki' => $manymanyimages,
                'post_id' => $last_id
            );

            return $this->db->insert('imgs', $data);
        }

1 个答案:

答案 0 :(得分:0)

删除for循环并添加foreach

$i = 0;
foreach($_FILES['files']['name'] as $row){
    $_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'];
    }
    $i++;
}