2012-05-07 64 views
1

我有以下的HTML代碼上傳多個文件使用HTML5多個屬性

<form action="form-handler.php" method="post" enctype="multipart/form-data"> 
    <div> 
      <input id="myfile" name="myfile" type="file"> 
      <input value="Upload ►" type="submit"> 
    </div> 
</form> 

我想一次上傳多個文件。我應該在控制器上做什麼來上傳codeigniter中的選定文件?我嘗試使用Codeigniter文件上傳庫,但它沒有加載多個文件。雖然,對於1個文件它工作正常。

+1

CI目前不支持多個文件。可能的重複:http://stackoverflow.com/questions/9276756/codeigniter-multiple-file-upload – Seabass

+0

所以你說沒有辦法上傳多個文件? –

+0

我的意思是,默認情況下,「CI目前不支持多個文件」,如果你檢查側面的鏈接,你會看到數百人發現了一種編輯核心以允許多個文件的方式。 – Seabass

回答

3

您可以在迭代遍歷發佈文件數組的foreach循環中調用Codigniter的do_upload()函數,$_FILE;

下面是在去接收上傳表單POST控制器功能可實現此代碼:

$this->load->library('upload'); 
$this->upload->initialize($config); //$config is array like as [in CI's documentation][1] 
$path='uploads/some_folder'; // refers to the root's uploads/some_folder folder 
$upload_data = $this->upload->multiFileUpload($path, TRUE); 

魔術multiFileUpload()功能,我從別人那裏得到。該函數在CI的上傳庫中進行了擴展,方法是將其放入我的/ application/libraries文件夾中名爲MY_Upload.php的文件中。

以下是MY_Upload.php的全部內容,它實際上只是multiFileUpload()的功能,所以不要被其他人知道,除了$path之外的任何地方你想要的文件。

<? 

class MY_Upload extends CI_Upload { 


public function multiFileUpload($path, $protect = FALSE){ 

    /* 
    * Declare uploaded_info and uploaded_files 
    * when i'm sure $_FILES has some data 
    */ 
/* if($this->upload_path[strlen($this->upload_path)-1] != '/') 
    $this->upload_path .= '/';*/ 

    //$this->upload_path=$path; 

    if(isset($_FILES)){ 

    #Here we check if the path exists if not then create 
    /*if(!file_exists($this->upload_path)){ 
    @mkdir($this->upload_path,0700,TRUE); 
    }*/ 

    if(!file_exists($path)){ 
    @mkdir($path,0700,TRUE); 
    } 
    $uploaded_info = FALSE; 
    /* 
    * The structure of $_FILES changes a lot with the array name on the input file, 
    * then i'm gonna modify $_FILES to make it think the data comes from several 
    * input file instead of one "arrayfied" input. 
    * 
    * The several ways to upload files are controled with this if...else structure 
    */ 
    if(count($_FILES) == 1) 
    { 
     $main_key = key($_FILES); 
     if(is_array($_FILES[$main_key]['name'])) 
     { 

      foreach($_FILES[$main_key] as $key => $value) 
      {     

       for($y = 0; $y < count($value); $y++) 
       { 

        $_FILES[$main_key .'-'. $y][$key] = $value[$y]; 

       } 


      } 

      unset($_FILES[$main_key]); 

      $uploaded_files = $_FILES; 
     } 
     else 
     { 
      $uploaded_files = $_FILES;  
     } 

    } 
    else 
    { 
     $uploaded_files = $_FILES;  
    } 

    #Here we create the index file in each path's directory 
    /*if($protect){ 
    $folder = ''; 
    foreach(explode('/',$this->upload_path) as $f){ 

    $folder .= $f.'/'; 
    $text = "<?php echo 'Directory access is forbidden.'; ?>"; 

    if(!file_exists($folder.'index.php')){ 
     $index = $folder.'index.php'; 
     $Handle = fopen($index, 'w'); 
     fwrite($Handle, trim($text)); 
     fclose($Handle); 
    } 
    } 
    }*/ 

    #Here we do the upload process 

    foreach($uploaded_files as $file => $value){ 
    if (!$this->do_upload($file)) 
    { 
    $uploaded_info['error'][] = array_merge($this->data(), 
       array('error_msg' => $this->display_errors())); 

    } 
    else 
    { 
    $uploaded_info['upload_data'][] = array_merge($this->data(), 
       array('error_msg' => $this->display_errors())); 
    } 
    } 
    } 

    #Then return what happened with the files 
    return $uploaded_info; 
} 
}