2017-04-24 66 views
1

我试图使用表单上传两个图像。为此我写了一个图像函数。Codeigniter - 两个图像上传不起作用

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

是否有人可以帮助我,谢谢你..

+0

我可以建议你一个完全不同的做法? – GeorgeGeorgitsis

+0

@satafaka是的请 – user3099298

+0

如果您发现我的回答很有帮助,请添加评论,我会尽力帮助您更好地使用您的代码。 – GeorgeGeorgitsis

回答

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 
    } 
} 
+0

虽然它的普通php,而不是CI上传的方式,但它总是有用的,它始终有效。我的upvote –

0

终于找到了解决办法,在笨的时候上传图片,我们必须调用,

$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; 
}