多张图片没有上传,总是得到第一张图片

时间:2018-11-08 07:18:25

标签: codeigniter-3

查看:

<div class="col-md-6">
        <div class="form-group">              
                <input type = "file" class="form-control"  name = "userfile" id="userfile"  size = "20" />
        </div>
 </div>
 <div class="col-md-6">          
        <div class="form-group">             
                <input type = "file" class="form-control"  name = "userfile1" id="userfile1"  size = "20" />
        </div>
 </div>

控制器:

 $this->do_upload_landscape($data['package']['pID'] ,$this->input->post('userfile'));
 $this->do_upload_thumb($data['package']['pID'] ,$this->input->post('userfile1'));

上传第一张图片的功能

public function do_upload_landscape($member_id,$imageland)
        {
            $data['package'] = $this->Package_model->get_package_for_edit($member_id);
            $new_name = $data['package']['pID'].$data['package']['packgeID'] ;
            $config['file_name'] = $new_name;
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000';
            $this->load->library('upload', $config);
            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());
                $data['_view'] = 'package/add';
                $this->load->view('admin/layouts/main',$data,$error);
            }
            else
            {
                $data = array('upload_data' => $this->upload->data($imageland));
                $file_data= $data['upload_data'];
                $params = array(
                    'files' => site_url().'uploads/'.$file_data['file_name']
                );
               $result= $this->Package_model->update_package($member_id,$params);
            }
        }

上传第二张图片的功能

public function do_upload_thumb($member_id,$thumb)
        {   
            $data1['package'] = $this->Package_model->get_package_for_edit($member_id);
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000';
            $this->load->library('upload', $config);
            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());
                $data1['_view'] = 'package/add';
                $this->load->view('admin/layouts/main',$data1,$error);
            }
            else
            {
                $data1 = array('upload_data' => $this->upload->data($thumb));
                $file_data= $data1['upload_data'];
                $params = array(

                    'files_thumb' => site_url().'uploads/'.$file_data['file_name']
                );
                $result= $this->Package_model->update_package($member_id,$params);

            }
        } 

问题:两次,我得到的都是相同的图片。从视图页面获得的图片值可以正常显示,但向上倾斜时,它始终会获取第一张图片的值。 在db中,如果第一个图片名称为56R45,那么第二个图片名称为56R451,这意味着相同的图片被添加了两次。谢谢

2 个答案:

答案 0 :(得分:0)

您需要在$this->upload->do_upload()中指定文件名。您应该传递输入元素的名称。例如

<input type = "file" class="form-control"  name = "userfile" id="userfile"  size = "20" />

然后,您必须使用$this->upload->do_upload()这样的文件名来调用$this->upload->do_upload('userfile')。与第二个文件输入元素相同。

提示:您应该重构文件上传代码。不必使用两个单独的函数,您可以创建一个函数并在需要时随时调用它。

答案 1 :(得分:-1)

您必须初始化上传文件并添加此文件

$this->upload->initialize($config);

之后

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

在这两个函数中都是这样

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