2011-12-04 71 views
15

在我的CodeIgniter項目中,我在項目創建期間上傳文件。這裏的上傳功能:在CodeIgniter中上傳多個文件

function uploadFiles(){ 

    $this->load->library('upload'); 
    $error = 0;  
    $projectName = $_POST['projectname']; 
    mkdir('./uploads/'.$projectName); 

    for($i=0; $i<count($_FILES); $i++) 
    { 

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

     $config['upload_path'] = './uploads/'.$projectName; 
     $config['allowed_types'] = 'xml|etl'; 
     $config['max_size']  = '0'; 
     $config['overwrite']  = TRUE; 

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

     if($this->upload->do_upload()) 
     { 
     $error += 0; 
     }else{ 
     $error += 1; 
     } 
    } 

    if($error > 0){ return FALSE; }else{ return TRUE; } 

} 

我呼籲在create_project功能,此功能是這樣的:

public function create_project() { 
    $this->load->library('form_validation'); 

    $this->form_validation->set_rules('projectname', 'Project name', 'required'); 

    $this->form_validation->set_message('required', 'This is an obligatory field'); 

    if($this->form_validation->run() == FALSE) { 
     $this->load->view('project_creation_view'); 
    } else { 
     $this->load->model('Project_management_model'); 
     $this->Project_management_model->create_project(); 
     $this->uploadFiles(); 
    } 
} 

然而,這並不做任何事情。這些文件沒有被上傳。即使是空目錄也沒有被創建。有人能幫我找到錯誤嗎?

謝謝。

+0

查看頁面roytuts.com/codeigniter-multiple-files-upload/ – user3470953

回答

20

您可以上傳任意數量的文件..

$config['upload_path'] = 'upload/Main_category_product/'; 
$path=$config['upload_path']; 
$config['allowed_types'] = 'gif|jpg|jpeg|png'; 
$config['max_size'] = '1024'; 
$config['max_width'] = '1920'; 
$config['max_height'] = '1280'; 
$this->load->library('upload', $config); 

foreach ($_FILES as $fieldname => $fileObject) //fieldname is the form field name 
{ 
    if (!empty($fileObject['name'])) 
    { 
     $this->upload->initialize($config); 
     if (!$this->upload->do_upload($fieldname)) 
     { 
      $errors = $this->upload->display_errors(); 
      flashMsg($errors); 
     } 
     else 
     { 
      // Code After Files Upload Success GOES HERE 
     } 
    } 
} 
+1

是否有文件數量的設置?我只能使用我的codeigniter代碼上傳20個,與此相比沒有多大區別。 – jason

+0

+1爲'$ this-> upload-> initialize()' – Jonah

+4

它給了我錯誤is_uploaded_file()期望參數1是字符串,給定數組,因爲我試圖調試,$ fieldname是一個數組。 – zoom

0

我想你錯過了這一點:

 
$config['file_name']  = 'somename_'.$i; 
$config['upload_path'] = './uploads/'.$projectName; 
... 
+0

謝謝。但它沒有幫助。 – cycero

2

這是我修改上傳多個文件,只是這個複製到MY_Upload.php文件CI_Upload類的擴展。它也使目錄。

http://pastebin.com/g9J6RxW1

然後,所有你需要在控制器功能做的是安排您的文件到一個數組,其中字段的名稱和關鍵是數組鍵和配置是數據。在你的情況下是這樣的:

$project_files['files'][0] = array(
      'upload_path' => './uploads/'.$projectName.'/', 
      'max_size' => 0, 
      'allowed_types' => 'xml|etl', 
      'overwrite' => TRUE, 
      'remove_spaces' => TRUE, 
     ); 
$project_files['files'][1] = array(
      'upload_path' => './uploads/'.$projectName.'/', 
      'max_size' => 0, 
      'allowed_types' => 'xml|etl', 
      'overwrite' => TRUE, 
      'remove_spaces' => TRUE, 
     ); 

如果所有文件的configs是一樣的只是做一個for循環來設置的,它也將採取「命名的鍵」,即。 $project_files['files']['file_key']

然後只要致電:

if($this->upload->upload_files($project_files)){/*all files uploaded successfully*/} 
0

思想我之前看到這個完全相同的問題在這裏;)

嘗試輸出$這個 - > upload->的display_errors()你在哪裏遞增$錯誤變量。那會告訴你究竟出了什麼問題。我的錢是在權限...

0

我修改立方眼的MY_upload返回上傳的所有文件的文件信息。就像這樣,調用data方法時只能訪問最後一個文件的信息。我還將上傳器的IP地址添加到該列表中。

http://pastebin.com/tG8A85gY

它可以通過下面的被利用:

$upload_files = $this->upload->upload_files($project_files); 
if ($upload_files->status == true) 
{ 
$fileInfo = $uploaded_files->file_info; 
} 

我意識到這將是更好的編輯到立方眼睛,但是這是我的第一篇文章,我沒有信譽。

0

爲了簡單起見,我創建了這個CI幫助文件。您可以從配置文件&中加載它在項目中的任何位置使用。它只是需要3個patrameters

@fileobject = HTML文件對象的名稱

@fileopath =您要的文件路徑上傳

@filetypes =默認*,EX-「JPG | PNG | MP4 | pdf'

function fileUploader($fileobject, $filepath = "uploads/", $filetypes = "*"){ 
// retrieve the number of images uploaded; 
$number_of_files = sizeof($_FILES[$fileobject]['tmp_name']); 

// considering that do_upload() accepts single files, we will have to do a small hack so that we can upload multiple files. For this we will have to keep the data of uploaded files in a variable, and redo the $_FILE. 
$files = $_FILES[$fileobject]; 

$errors = array(); 
$file_arr = array(); 
$result = array(); 

// first make sure that there is no error in uploading the files 
for ($k = 0; $k < $number_of_files; $k++) { 
    if ($_FILES[$fileobject]['error'][$k] != 0) $errors[$k][] = 'Couldn\'t upload file ' . $_FILES[$fileobject]['name'][$k]; 
} 
if (sizeof($errors) == 0) { 
    // now, taking into account that there can be more than one file, for each file we will have to do the upload 
    // we first load the upload library 
    $CI =& get_instance(); 

    $CI->load->library('upload'); // load library 

    // next we pass the upload path for the images 
    $config['upload_path'] = FCPATH . $filepath; 
    // also, we make sure we allow only certain type of images 
    $config['allowed_types'] = $filetypes; 
    //$config['max_size'] = (int)(ini_get('upload_max_filesize')); 
    $config['max_size'] = '10000'; 
    $config['overwrite'] = FALSE; 

    for ($i = 0; $i < $number_of_files; $i++) { 
     $_FILES['uploadedimage']['name'] = $files['name'][$i]; 
     $_FILES['uploadedimage']['type'] = $files['type'][$i]; 
     $_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i]; 
     $_FILES['uploadedimage']['error'] = $files['error'][$i]; 
     $_FILES['uploadedimage']['size'] = $files['size'][$i]; 
     //now we initialize the upload library 
     $CI->upload->initialize($config); 
     // we retrieve the number of files that were uploaded 
     if ($CI->upload->do_upload('uploadedimage')) { 
      $imageup = $CI->upload->data(); 
      // print_r($imageup["orig_name"]);die; 
      array_push($file_arr, $imageup["orig_name"]); 
      $result["response"] = "true"; 
      $result["message"] = "Files uploaded successfully."; 
      $result["files"] = $file_arr; 

     } else { 
      $result["response"] = "false"; 
      $result["message"][$i] = $CI->upload->display_errors(); 
     } 
    } 

} else { 
    $result["response"] = "false"; 
    $result["message"] = $errors; 

} 
return $result;} 

測試&已被證實。

1

由於我的聲望級別,我還不能評論,但Zoom在接受的關於$ fieldname變量的答案下做了一個評論,該變量是導致錯誤的數組。我對該答案有同樣的問題,並發現這是因爲我用[]將所有文件輸入名稱附加在表單上,​​這使得php以數組變量的形式提取這些輸入。要解決該問題,只需爲每個文件輸入一個唯一的名稱,而不是使其成爲一個PHP數組。我這樣做後,這個問題對我來說就消失了,接受的答案就像一個魅力。只是以爲我會把這些信息一起傳給那些在這個問題上磕磕絆絆的人。

+0

你是什麼意思給每個文件輸入一個唯一的名字?使用單個「'並使用多個輸入字段行是有區別的。使用單個輸入字段,您可以從文件瀏覽器窗口中選擇多個文件。有了多個輸入字段,您一次只能選擇一個文件,而且我認爲這會降低選擇多個文件的功能,但我想它是有效的。也許你可以解釋一下你的意思? – TARKUS

+0

另外,我也使用'name ='文件[]'',所以我知道你在駕駛什麼。在Ajax調用中,我循環遍歷文件[],如下所示:'formData.append('date',document.getElementById('date').value); var ins = document.getElementById('files')。files.length; (var x = 0; x "; print_r($_FILES); echo "」;'來檢查,但上面所接受的答案不適用於我,所以我仍然做錯了什麼。 :/ – TARKUS