在Codeigniter中上传多个图像

时间:2014-05-21 13:11:56

标签: php codeigniter oop codeigniter-2 codeigniter-form-helper

我试图从HTML FORM上传多张图片但是只提交上传的图片,请任何能解决此问题的人

enter image description here

这是我的控制器

if($_FILES['image']['name'] != "")
{
    $data['image'] = $this->MUtils->doUpload('image',270,65,false);
}
if($_FILES['adv_image1']['name']!= "")
{
    $data['adv_image1'] = $this->MUtils->doUpload('adv_image1',340,130,false);
}
if($_FILES['adv_image2']['name']!= "")
{
    $data['adv_image2'] = $this->MUtils->doUpload('adv_image2',860,100,false);
}

模型是

    if($data['image']!="" ){
                $arr=array('image' => $data['image']);
            }
            if($data['adv_image1']!=""){
                $arr=array('adv_image1' => $data['adv_image1']);
            }
            if($data['adv_image2']!=""){
                $arr=array('adv_image2' => $data['adv_image2']);
            }

if($data['adv_image3']!=""){
            $arr['adv_image3'] = $data['adv_image3'];
        }

        $this->db->where('id',$data['listid']);
        $this->db->update('list', $arr);

        return 1;

doUpload Functio就在这里

  //Upload file and return url
    function doUpload($field, $width, $height, $resize=false)
    {
        //Configure upload.
        $this->upload->initialize(array(
            "upload_path"   => "../uploads/",
            "allowed_types" => "gif|jpg|png",
        ));


        //Perform upload.
        if($this->upload->do_upload($field)){

            $fileData = $this->upload->data();

            if ($resize == true)
            {
                $width = $fileData['image_width'];
                $height = $fileData['image_height'];
            }


            $img_cfg_thumb['image_library'] = 'gd2';
            $img_cfg_thumb['source_image'] = "../uploads/" . $fileData['raw_name'] . $fileData['file_ext'];
            $img_cfg_thumb['maintain_ratio'] = FALSE;
            $img_cfg_thumb['new_image'] = "../uploads/" . $fileData['raw_name'] . $fileData['file_ext'];
            $img_cfg_thumb['width'] = $width;
            $img_cfg_thumb['height'] = $height;
            $img_cfg_thumb['quality'] = 90;
            $this->load->library('image_lib');
            $this->image_lib->initialize($img_cfg_thumb);
            $this->image_lib->resize();


            return $fileData['raw_name'] . $fileData['file_ext'];
        }
        else
        {
            return "";
        }
    }

注意这是有效的,但是从三个图片中,最后一个上传到提交

2 个答案:

答案 0 :(得分:1)

在您的模型中更改以下行:

if($data['image']!="" ){
    $arr=array('image' => $data['image']);
}
if($data['adv_image1']!=""){
    $arr=array('adv_image1' => $data['adv_image1']);
}
if($data['adv_image2']!=""){
    $arr=array('adv_image2' => $data['adv_image2']);
}
if($data['adv_image3']!=""){
    $arr=array('adv_image3' => $data['adv_image3']);
}

到这些方面:

if($data['image']!="" ){
    $arr['image'] = $data['image'];
}
if($data['adv_image1']!=""){
    $arr['adv_image1'] = $data['adv_image1'];
}
if($data['adv_image2']!=""){
    $arr['adv_image2'] = $data['adv_image2'];
}
if($data['adv_image3']!=""){
    $arr['adv_image3'] = $data['adv_image3'];
}

答案 1 :(得分:-1)

每次都需要清除配置和库。

在启动该功能时尝试以下代码:

unset($config)
$this->upload->clear();

希望这有帮助