如何使用codeigniter上传表单中的两个图像

时间:2013-11-29 12:17:11

标签: codeigniter

拥有像这样的表单字段

<form>
 Image 1 <input type='file' name="userfile">

 Image 2 <input type='file' name="userfile1"/>
</form>

上传图片1的控制器功能

public function profile_imageupload()
    {        
        $random_no = rand();        
        $filename = date('YmdHis')."_".$random_no;
        $config = array(
        'upload_path'   =>'./uploads/original/',  
        'allowed_types' => 'gif|jpg|png',
        'max_size'      =>'10000', 
        'max_width'     =>'2000',
        'max_height'   =>'2000',
        'file_name'    =>$filename
        );  

        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $this->load->library('image_lib');
        if ( ! $this->upload->do_upload()){
            $error = array('error' => $this->upload->display_errors()); 
        }else{
            $data = array('upload_data' => $this->upload->data());        

            $path_info = pathinfo($data['upload_data']['full_path']);
            $fileExtension = $path_info['extension'];
            return  $config['file_name'].".".$fileExtension;
        }
    }

在此只有一张图片上传(第一张图片)第二张图片未上传到文件夹中。 如何在另一个位置插入第二个

1 个答案:

答案 0 :(得分:0)

试试这个

<form>
 Image 1 <input type='file' name="userfile[]">

 Image 2 <input type='file' name="userfile[]"/>
</form>


    public function profile_imageupload()
        {        

           $config['upload_path'] = 'upload/';
           $path=$config['upload_path'];
           $config['allowed_types'] = 'gif|jpg|jpeg|png';
           $config['max_size'] = '1024';
           $config['max_width'] = '1920';
           $config['max_height'] = '1280';
           $this->load->library('upload');

           foreach ($_FILES as $fieldname => $fileObject)  //fieldname is the form field name
           {
             if (!empty($fileObject['name']))
             {
                $this->upload->initialize($config);
                if (!$this->upload->do_upload($fieldname))
                {
                    $errors = $this->upload->display_errors();
                    flashMsg($errors);
                }
                else
                {
                    // Code After Files Upload Success GOES HERE
                }
             }
        }
    }