2013-01-24 64 views
2

我正在使用此功能上傳圖像,並且它正在工作,除了一部分。如果有多個圖片用於上傳,所有圖片都會從第一張圖片中獲取其名稱(覆蓋設置爲關閉,因此CI將在名稱末尾添加數字)。我怎麼解決這個問題?Codeigniter 2.1 - 圖像名稱在上傳時無法正常工作

function img_upload($folder) { 
    $this->path = './public/img/' . $folder; 
    $imgs = array(); 
    $count = 0; 
    foreach($_FILES as $key => $value): 
     $img_name = is_array($value['name']) ? $value['name'][$count] : $value['name']; 
     $img_name = $this->char_replace($img_name, '_'); 
     $count++; 
     $config = array(
      'allowed_types' => 'jpg|jpeg|png|gif', 
      'upload_path' => $this->path, 
      'file_name' => $img_name 
     ); 
     $this->CI->load->library('image_lib'); 
     $this->CI->image_lib->clear(); 
     $this->CI->load->library('upload', $config); 
     if($key != 'logo'): 
      if (!$this->CI->upload->do_upload($key)) { 
      } else { 
       $image = $this->CI->upload->data(); 
       $imgs[] = $image['file_name']; 
      } 
     endif; 
    endforeach; 

    if(empty($imgs)): 
     return FALSE; 
    else: 
     return implode(',', $imgs); 
    endif; 
} 

功能char_replace工作沒有問題。

function char_replace($text, $rep_simbol = " ") 
{ 
    $char = array('!', '&', '?', '/', '/\/', ':', ';', '#', '<', '>', '=', '^', '@', '~', '`', '[', ']', '{', '}'); 
    return $name = str_replace($char, $rep_simbol, $text); 
} 
+0

請不要將'if():endif;'語法與'if(){}'語法混合使用! –

+0

@RocketHazmat我沒有看到。我正在改變這個功能,並且一些舊代碼保持不變。感謝您的洞察力。 – Sasha

+0

您的'$ count'變量只在每次運行'foreach'時增加。你想要一個內部循環來遍歷多個文件。 –

回答

1

$this->CI->upload->do_upload($key)預計$_FILES['key']只包含一個文件。

你可以做的是,複製$_FILES,循環遍歷它,併爲每個文件設置值爲$_FILES['key']

function img_upload($folder) { 
    $this->path = './public/img/' . $folder; 
    $imgs = array(); 

    // Copy of $_FILES 
    $thisFiles = $_FILES; 

    // Loop through copy of $_FILES 
    foreach($theFiles as $key => &$value){ 
     // Create the $_FILES array for each individual file, 
     // so that do_upload can read it correctly 
     if(!is_array($value['name'])){ 
      // If it's not an array, make it one, 
      // this will make our future code easier 
      foreach($value as $kv => &$val){ 
       $val = array($val); 
      } 
     } 

     // Loop through each file and upload each one 
     foreach($value['name'] as $count=>$img_name){ 
      $img_name = $this->char_replace($img_name, '_'); 

      foreach($_FILES[$key] as $k => &$v){ 
       // CodeIgniter will think this is the $_FILES array 
       $v = $theFiles[$key][$k][$count]; 
      } 

      $config = array(
       'allowed_types' => 'jpg|jpeg|png|gif', 
       'upload_path' => $this->path, 
       'file_name' => $img_name 
      ); 

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

      if($key != 'logo'){ 
       if (!$this->CI->upload->do_upload($key)) { 
       } 
       else { 
        $image = $this->CI->upload->data(); 
        $imgs[] = $image['file_name']; 
       } 
      } 
     } 
    } 

    return !empty($imgs) ? implode(',', $imgs) : FALSE; 
} 

注意:這是未經測試

+0

它需要一些調整,但我看到了一般想法。謝謝你的幫助:) – Sasha

+0

就像我說的,它沒有經過測試。這基本上就是你需要做的,因爲'$ this-> CI-> upload-> do_upload($ key)'假設'$ _FILES ['key']'只有一個文件。 –