2014-03-02 40 views
0

我已經生成了一個代碼,它將在codeigniter中上傳多個圖像,但是我無法使用codeigniter圖像庫生成大尺寸的上傳圖像到其他目錄。上傳多個圖像並生成大尺寸

使用下面的代碼,即使用戶上傳了多個圖像,它也只會生成一個圖像。

function insert_prod(){ 

    $foldername = $_POST['folder']; 
    $path = './uploads/products/'.$foldername; 

    mkdir($path, 0777, true); 
    mkdir($path.'/zoomimages', 0777, true); 

    $config = array(); 
    $config['upload_path'] = $path; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size']  = '0'; 
    $config['overwrite']  = FALSE; 

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

    $files = $_FILES; 
    $cpt = count($_FILES['userfile']['name']); 
    for($i=0; $i<$cpt; $i++){ 

     $_FILES['userfile']['name']= $files['userfile']['name'][$i]; 
     $_FILES['userfile']['type']= $files['userfile']['type'][$i]; 
     $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; 
     $_FILES['userfile']['error']= $files['userfile']['error'][$i]; 
     $_FILES['userfile']['size']= $files['userfile']['size'][$i];  

     $config['file_name'] = 'img_'.$i; 

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

     $upload_data = array('upload_data' => $this->upload->data()); 
     $full_path = $upload_data['upload_data']['full_path']; 
     $u_data = $upload_data['upload_data']; 
     // var_dump($this->upload->data()); 
     // var_dump($upload_data); 
     echo $cpt; 
     $config1['image_library'] = 'gd2'; 
     $config1['source_image'] = $full_path; 
     $config1['new_image'] =$path.'/zoomimages'; 
     $config1['create_thumb'] = FALSE; 
     $config1['maintain_ratio'] = TRUE; 
     $config1['width'] = 300; 
     $config1['height'] = 300; 

     $this->load->library('image_lib', $config1); 
     $this->image_lib->resize(); 
     $this->image_lib->clear(); 
    } 
} 
+0

對於每個上傳指定輸入名稱'$ this-> upload-> do_upload('NAME');' – Minhaz

+0

檢查此http://stackoverflow.com/questions/11 524356/multiple-files-upload-array-with-codeigniter-2-0 – Minhaz

+0

我正在使用該代碼。我可以上傳多張圖片,但我想爲圖片生成一個大尺寸,並將其放在其他目錄中。 –

回答

0

您正在加載庫$this->load->library('image_lib', $config1);您的循環中這樣笨看到類已經加載所以它不是設置你的$config1 again

移動$this->load->library('image_lib');出方的迴路

然後加入$this->image_lib->initialize($config1);瞬間$this->load->library('image_lib', $config1);