如何在codeigniter中上传多个/多个图像

时间:2018-02-21 13:15:57

标签: php codeigniter phpmyadmin codeigniter-2 codeigniter-3

我的源代码有问题,当输入很多图像和不同的图像/上传文件时,获得的结果是相同的图像。我该如何解决,请帮帮我

CONTROLLER

public function tambahprogress()
{
    $this->load->helper('url','form');
    $this->load->library('form_validation');
    $this->form_validation->set_rules('keterangan','keterangan','trim|required');

    $this->load->model('progress/progress_model');

    if ($this->form_validation->run()==FALSE) {
        $data['pekerjaan']=$this->progress_model->combopekerjaan();
        $data['pekerja']=$this->progress_model->combopekerja();
        $this->load->view('progress/tambahprogress', $data);
    }else{
        $config['upload_path']      = './assets/upload/';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']         = 1000000000;
        $config['max_width']        = 10240;
        $config['max_height']       = 7680;

        $this->load->library('upload', $config);
        if (! $this->upload->do_upload()) 
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('progress/tambahprogress',$error);
        }
        else
        {
            $image_data = $this->upload->data();
            $configer = array (
                'image_library'=>'gd2',
                'source_image'=>$image_data['full_path'],
                'width'=>800,
                'height'=>600,
                );
            $this->load->library('image_lib',$config);
            $this->image_lib->clear();
            $this->image_lib->initialize($configer);
            $this->image_lib->resize();
            $this->progress_model->insertProgress();
            $this->load->view('progress/tambahprogresssukses');
        }
    }
}

MODEL

public function insertProgress()
{
    $object = array
    (
        'id_progress' =>$this->input->post('id_progress'), 'no_spk' =>$this->input->post('no_spk'),'progress' =>$this->input->post('progress'), 'keterangan' =>$this->input->post('keterangan'), 'gambar'=>$this->upload->data('file_name'), 'gambar2'=>$this->upload->data('file_name'), 'gambar3'=>$this->upload->data('file_name'), 'id_pekerja' =>$this->input->post('id_pekerja')
    );
    $this->db->insert('progress',$object);
}

查看(输入过程)

<div class="form-group">
    <label for="">Masukkan Gambar</label>
    <<input type="file" name="userfile" size="20"/>
</div>
<div class="form-group">
    <input type="file" name="userfile" size="20"/>
</div>
<div class="form-group">
    <input type="file" name="userfile" size="20"/>
</div>

,结果是相同的图像但是当我输入数据时,我输入不同的图像请帮帮我

1 个答案:

答案 0 :(得分:0)

 $config['upload_path'] = PATH; //add path according to your requirements 
 $config['allowed_types'] = 'jpg|jpeg|png';
 $config['overwrite'] = false; //OR true
 $config['max_size'] = '100000'; //You can change it
 //add more configuration according to your requirements 
 $this->load->library('upload');
 $files = $_FILES;
 $number_of_files = count($_FILES['pic']['name']); //"pic" is name of FILE input 
 //images name will be details0, details1, details2 and soo on.
 $errors = 0;
 for ($i = 0; $i < $number_of_files; $i++) {
 $_FILES['pic']['name'] = "details" . $i . ".jpg"; //If you want to change the name of images change "details" with your require name
 $_FILES['pic']['type'] = $files['pic']['type'][$i];
 $_FILES['pic']['tmp_name'] = $files['pic']['tmp_name'][$i];
 $_FILES['pic']['error'] = $files['pic']['error'][$i];
 $_FILES['pic']['size'] = $files['pic']['size'][$i];
 $this->upload->initialize($config);
 if (!$this->upload->do_upload("pic")) 
 {
   $errors++;
 }
} 
if ($errors > 0) 
{
   echo $errors . "File(s) could not be uploaded";
}