2014-01-29 44 views
1

兩個圖像文件到兩個獨立的文件夾,我有兩個文件輸入字段我想在我的HTML表單上傳使用笨

<div style="width: 125px; height: 35px; float: left; font-weight: bold; font-size: 16px;"> 
        Image Large 
       </div> 
       <div style="width: 125px; height: 35px; float: left; margin-top: 15px;"> 
        <input type="file" name="large_image" id="large_image"/> 
       </div> 
       <div style="clear: both;"></div> 

       <div style="width: 125px; height: 35px; float: left; font-weight: bold; font-size: 16px;"> 
        Image Small 
       </div> 
       <div style="width: 125px; height: 35px; float: left; margin-top: 15px;"> 
        <input type="file" name="small_image" id="small_image"/> 
       </div> 

在我的控制器文件我有兩個功能上傳小型和大型圖像。這兩個功能是通過一個單獨的功能等

private function insert_products() { 
$this->upload_prodct_img_small(); 
     $this->upload_prodct_img_large(); 
} 

兩個上載功能是這樣執行的。

private function upload_prodct_img_large() { 
     $image_name = $this->get_image_name_uploade(); 
     $config['upload_path'] = 'uploaded/large/'; 
     $config['file_name'] = $image_name; 
     $config['overwrite'] = 'FALSE'; 
     $config['allowed_types'] = 'png|PNG'; 
     //$config['max_size'] = '1000'; 
     //$config['max_width'] = '720'; 
     //$config['max_height'] = '960'; 
     $config['remove_spaces'] = TRUE; 
     $this->load->library('upload', $config); 

     if (!$this->upload->do_upload('large_image')) { 
      $error = array('error' => $this->upload->display_errors()); 
      $this->session->set_flashdata('error', $error); 
     } else { 
      $error = 'Successfully Uploaded'; 
      $this->session->set_flashdata('error', $error); 
     } 
    } 

    private function upload_prodct_img_small() { 
     $image_name_small = $this->get_image_name_uploade(); 
     $config['upload_path'] = 'uploaded/'; 
     $config['file_name'] = $image_name_small ; 
     $config['overwrite'] = 'FALSE'; 
     $config['allowed_types'] = 'png|PNG'; 
     //$config['max_size'] = '1000'; 
     //$config['max_width'] = '180'; 
     //$config['max_height'] = '240'; 
     $config['remove_spaces'] = TRUE; 
     $this->load->library('upload', $config); 

     if (!$this->upload->do_upload('small_image')) { 
      $error = array('error' => $this->upload->display_errors()); 
      $this->session->set_flashdata('error', $error); 
     } else { 
      $error = 'Successfully Uploaded'; 
      $this->session->set_flashdata('error', $error); 
     } 
    } 

在這兩種情況下,我將文件重命名爲$ config數組中的單個文件名。

問題是這樣的。當我試圖上傳這兩個文件時,只有「small_image」正在上傳,並且總是缺少另一個文件。 你可以看到我已經註釋到了寬度的高度和尺寸。因爲如果我沒有這樣做,它會給我一個錯誤,說「你上傳的文件超過了允許的高度或寬度」(類似的)。
我有雙重檢查,我上傳的文件大小是完全相同的允許。

另一種情況是我做了這樣的事情。

private function insert_products() { 
    //$this->upload_prodct_img_small(); 
     $this->upload_prodct_img_large(); 
    } 

然後大圖像上傳沒有任何錯誤。

找不到我做錯了什麼?

+0

10次仍沒有回覆:( – Yasitha

+0

你給premissions到large_image文件夾? –

+0

是存在的權限沒有錯誤。因爲當我嘗試只上傳大的圖像是沒有錯誤 – Yasitha

回答

1

這是我使用的,我測試了它,它適用於我剛剛閱讀的評論。

set_time_limit(0); 
$config['upload_path'] = './assets/uploads/pictures'; 
$config['allowed_types'] = 'png'; 
$config['max_size'] = '1000'; 
$config['width']  = 188; 
$config['height'] = 187; 

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

if (! $this->upload->do_upload()) 
{ 
    $error = array('error' => $this->upload->display_errors()); 
    $this->view_data['system_message'] = $this->_msg('n',' '.$error['error']); 
} 
else 
{ 
    //if data has been uploaded successfully get the newly uploaded file and process 
    //this is the data for the newly uploaded file 
    $data = array('upload_data' => $this->upload->data()); 
    //resize start 
    $source_path = $data['upload_data']['full_path']; 
    //new path i want to put the resized image 
    $target_path = './assets/uploads/thumbs'; 

    //just create thumbs 
    $thumb['image_library'] = 'gd2'; 
    $thumb['source_image'] = $source_path; 
    $thumb['new_image'] = $target_path; 
    $thumb['create_thumb'] = TRUE; 
    $thumb['maintain_ratio'] = FALSE; 
    $thumb['width'] = 100; 
    $thumb['height'] = 100; 
    $this->image_lib->initialize($thumb); 
    $this->image_lib->resize(); 
    $this->image_lib->clear(); 

    //resize image 
    $thumb2['image_library'] = 'gd2'; 
    $thumb2['source_image'] = $source_path; 
    $thumb2['new_image'] = $data['upload_data']['file_path']; 
    $thumb2['create_thumb'] = FALSE; 
    $thumb2['maintain_ratio'] = TRUE; 
    $thumb2['width'] = 200; 
    $thumb2['height'] = 200; 
    $this->image_lib->initialize($thumb2); 
    $this->image_lib->resize(); 
    $this->image_lib->clear(); 
    //resize end 
2

只是在第二個函數中加載上傳庫之前初始化新配置數組。前行

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

即通過包括線初始化$配置

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

一般情況下,你總是有使用它與一個庫之前,以初始化新配置,否則,默認配置將被使用。在你的情況下,默認是你的第一個配置。

+0

@Yasitha:希望你得到了你的答案! –

相關問題