2013-02-13 57 views
0

我正在使用下面的函數將我的圖像文件上傳到我的服務器(Localhost)。這是好的,圖像正在上傳。但是我需要兩個圖像域,因此一旦點擊提交按鈕,圖像就應該被上傳。我用這裏描述的功能Codeigniter multiple file upload,但它不工作。在codeigniter中上傳圖像,是否需要userfile?

我收到此錯誤信息

A PHP Error was encountered 

Severity: Warning 

Message: is_uploaded_file() expects parameter 1 to be string, array given 

Filename: libraries/Upload.php 

Line Number: 161 

嗯,我無法理解錯誤所在。 ,我使用上傳單個圖像的功能是

function do_upload() 
    { 
     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'jpeg|png|gif'; 
     $config['max_size'] = '0'; 
     $config['max_width'] = '0'; 
     $config['max_height'] = '0'; 

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

     if (! $this->upload->do_upload()) 
     { 
      $error = array('error' => $this->upload->display_errors()); 
      //failed display the errors 
     } 
     else 
     { 
      //success 

     } 
    } 

另外我還想問我可以改變我的選擇的輸入字段名。即我總是需要執行<input type="file" name="userfile"/>。是否可以更改名稱?我試着改變它,我收到消息沒有選擇文件,所以這意味着我不能改變它。

回答

5

你必須循環上傳的文件,就像它在你提供的鏈接中顯示的那樣。

function do_upload() { 
     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'jpeg|png|gif'; 
     $config['max_size'] = '0'; 
     $config['max_width'] = '0'; 
     $config['max_height'] = '0'; 

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

     foreach ($_FILES as $key => $value) { 

      if (!empty($value['tmp_name'])) { 

       if (! $this->upload->do_upload($key)) { 
        $error = array('error' => $this->upload->display_errors()); 
        //failed display the errors 
       } else { 
        //success 
       } 

      } 
     } 
} 

並嘗試使用HTML這樣的:

<input type="file" name="file1" id="file_1" /> 
<input type="file" name="file2" id="file_2" /> 
<input type="file" name="file3" id="file_3" /> 

,只要你喜歡你可以改變輸入字段的名稱。

+0

我仍然收到我在問題中發佈的錯誤消息。 – prakashchhetri 2013-02-13 21:22:38

+0

修正了一個錯誤。 – 2013-02-13 21:28:18

+0

它只上傳一張圖片 – prakashchhetri 2013-02-13 22:04:58

0

如果您正在使用的文件多選,那麼這樣做:

HTML(HTML5文件與數組名輸入允許選擇多個文件):

<input type="file" name="userfile[]"/> 

<input type="file" name="userfile[]"/> 
<input type="file" name="userfile[]"/> 

PHP in CodeIgniter:

// load upload library (put your own settings there) 
$this->load->library('upload', array('allowed_types' => 'svg|gif|jpg|png', 'upload_path' => $GLOBALS['config']['upload_path'],)); 

// normalise files array 
$input_name = 'userfile'; // change it when needed to match your html 
$field_names = array('name', 'type', 'tmp_name', 'error', 'size',); 
$keys = array(); 
foreach($field_names as $field_name){ 
    if (isset($_FILES[$input_name][$field_name])) foreach($_FILES[$input_name][$field_name] as $key => $value){ 
     $_FILES[$input_name.'_'.$key][$field_name] = $value; 
     $keys[$key] = $key; 
    } 
} 
unset($_FILES[$input_name]); // just in case 

foreach ($keys as $key){ 

    $new_file = $this->upload->do_upload($input_name.'_'.$key); 

    // do your stuff with each uploaded file here, delete for example: 
    $upload_data = $this->upload->data(); 
    unlink($upload_data['file_name']); 

}