2012-06-06 70 views
0

這是文件上傳的基本功能。 我需要它返回一個包含所有圖像名稱的字符串,並且如果沒有要上傳的圖像返回FALSE。我怎樣才能做到這一點?PHP,Codeigniter 2.1 - 多文件上傳和數組返回

function img_upload($folder) { 
      $this->path = './public/img/' . $folder; 
      $imgs = array(); 
      $g = true; 
      $config = array(
       'allowed_types' => 'jpg|jpeg|png|gif', 
       'upload_path' => $this->path 
      ); 
      $CI = &get_instance(); 
      $CI->load->library('upload', $config); 
      foreach ($_FILES as $key => $value) { 

       if (!$CI->upload->do_upload($key)) { 
        return FALSE; 
       } else { 
        $q = $CI->upload->data(); 
        $config['image_library'] = 'gd2'; 
        $config['source_image'] = $this->path . '/' . $q['file_name']; 
        $config['new_image'] = $this->path . '/thumbs'; 
        $config['create_thumb'] = FALSE; 
        $config['maintain_ratio'] = TRUE; 
        $config['width'] = 128; 
        $config['height'] = 128; 

        $CI->load->library('image_lib', $config); 
        $CI->image_lib->resize(); 
        echo $q['file_name'];     
        $imgs = array_push($imgs, $q['file_name']); 
       }  
      }   
     } 

回答

1

如果我理解正確的,你比這個代碼可以幫助你..

function img_upload($folder) { 
      $this->path = './public/img/' . $folder; 
      $imgs = array(); 
      $g = true; 
      $config = array(
       'allowed_types' => 'jpg|jpeg|png|gif', 
       'upload_path' => $this->path 
      ); 
      $CI = &get_instance(); 
      $CI->load->library('upload', $config); 
      foreach ($_FILES as $key => $value) { 

       if (!$CI->upload->do_upload($key)) { 
        return FALSE; 
      } else { 
       $q = $CI->upload->data(); 
       $config['image_library'] = 'gd2'; 
       $config['source_image'] = $this->path . '/' . $q['file_name']; 
       $allimages = array(); 
       $allimages = $q['file_name']; 
       $config['new_image'] = $this->path . '/thumbs'; 
       $config['create_thumb'] = FALSE; 
       $config['maintain_ratio'] = TRUE; 
       $config['width'] = 128; 
       $config['height'] = 128; 

       $CI->load->library('image_lib', $config); 
       $CI->image_lib->resize(); 
       echo $q['file_name'];     
       $imgs = array_push($imgs, $q['file_name']); 


       if(count($allimages) > 0) 
        { 
        return $allimagename = ('',$allimages); 
        } 
       else 
        { 
        return 'Thre is no images'; 
        } 
       }  
      }   
     } 
+0

這是它。謝謝你的幫助:) – Sasha

+0

@Sasha你的歡迎..... :) –