Codeigniter - 两张图片上传无效

时间:2017-04-24 09:28:57

标签: php codeigniter file-upload

我尝试使用表单上传两张图片。为此,我写了一个图像功能。

protected function imageUploadResize($filename, $field_name, $width, $height, $maker){
    // thumb image upload settings
    $config['upload_path']          = './uploads/packages/';
    $config['allowed_types']        = 'gif|jpg|png';
    // thumb upload settings
    $image_new_name = time().'_'.$filename;                    
    $config['file_name'] = $image_new_name; 
    $this->load->library('upload', $config);
    // upload thumb image
    if ( $this->upload->do_upload($field_name)) {
        // resize uploaded file
        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/packages/'.$image_new_name;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']         = $width;
        $config['height']       = $height;
        $config['thumb_marker'] = $maker;
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();    

        $error = array('error' => $this->upload->display_errors());                   
    } else {
       $imageData = array('upload_data' => $this->upload->data());
    }  
    return $image_new_name;
}

我以这种方式访问​​该功能,

// allocate image name to data set
if(!empty($_FILES["thumb_image"]['name'])){
    // thumb upload
    $thumb_image_new_name = $this->imageUploadResize($_FILES["thumb_image"]['name'],'thumb_image',THUMB_IMAGE_WIDTH, THUMB_IMAGE_HEIGHT, '_thumb');                    
    $data['thumb_image'] = $thumb_image_new_name;
}else{
    $data['thumb_image'] = "";
}
// allocate image name to data set
if(!empty($_FILES["banner_image"]['name'])){
    // banner upload
    $banner_image_new_name = $this->imageUploadResize($_FILES["banner_image"]['name'],'banner_image',BANNER_IMAGE_WIDTH, BANNER_IMAGE_HEIGHT, '_banner');                    
    $data['banner_image'] = $banner_image_new_name;
}else{
    $data['banner_image'] = "";
} 

当我上传一个图像(thumb_image或banner_image)时,上面的功能正常工作。

但是当我上传两张图片时,thumb_image已正确上传,但banner_image无法正常上传。举个例子,假设我要上传Hydrangeas.jpg和Desert.jpg。然后就是这样,

然后以这种方式上传文件(文件夹的图像名称),

  • 1493025280_Hydrangeas.jpg
  • 1493025280_Hydrangeas_thumb.jpg
  • 1493025280_Hydrangeas1.jpg - 图片名称也错误

但预期的输出是(文件夹的图像名称),

  • 1493025280_Hydrangeas.jpg
  • 1493025280_Hydrangeas_thumb.jpg
  • 1493025280_Desert.jpg
  • 1493025280_Desert_banner.jpg

有人可以帮助我,谢谢你。

2 个答案:

答案 0 :(得分:3)

我用于文件上传的不同方法如下。 我重新排列了$ _FILES数组,因为原始文件在键和对象方面存在一些困难。

$files = array();
   foreach ($_FILES['files']['name'] as $num_key => $dummy) {
      foreach ($_FILES['files'] as $txt_key => $dummy) {
         $files[$num_key][$txt_key] = $_FILES['files'][$txt_key][$num_key];
      }
   }

在新数组中,遍历每个图像并执行您需要的操作:

foreach ($files as $file) {
    if ($file['error'] == 0) {
          //Your code here...
          //Call your function imageUploadResize
    }
}

答案 1 :(得分:0)

终于找到了解决方案,在codeigniter上传图片时,我们要调用,

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

然后我们必须初始化配置。

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

当调整大小图像时,我们必须调用,

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

然后必须初始化配置。

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

因此完成的功能是,

protected function imageUploadResize($filename, $field_name, $width, $height, $maker){
    // thumb image upload settings
    $config['upload_path']          = './uploads/packages/';
    $config['allowed_types']        = 'gif|jpg|png';
    // thumb upload settings
    $image_new_name = time().'_'.$filename;                    
    $config['file_name'] = $image_new_name; 
    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    // upload thumb image
    if ( $this->upload->do_upload($field_name)) {
        // resize uploaded file
        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/packages/'.$image_new_name;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']         = $width;
        $config['height']       = $height;
        $config['thumb_marker'] = $maker;
        $this->load->library('image_lib', $config);
        $this->image_lib->initialize($config);
        $this->image_lib->resize(); 

        $error = array('error' => $this->upload->display_errors());                   
    } else {
       $imageData = array('upload_data' => $this->upload->data());
    }  
    return $image_new_name;
}