2011-11-01 55 views
0

首先,我不如何使用CodeIgniter的執行多文件上傳,而是如何防止任何/所有文件,如果最後一個線路故障正在上傳(由於文件類型,大小等)。基本上我希望事先使用CI的文件驗證(在上傳任何內容之前),如果一切正常,請執行所有上傳。笨多文件上傳

我有一個表單上傳兩個文件。如果第一個文件通過驗證,並且do_upload()方法返回TRUE,但第二個文件失敗,則第一個文件仍然上傳 - 如何防止此問題?

這裏是我的代碼:

// Controller 

function upload_files() 
{ 

    // Configure first file upload 
    $config['upload_path']   = 'public/mp3/'; 
    $config['overwrite']   = FALSE; 
    $config['allowed_types']  = 'mp3'; 
    $config['max_size']    = '20000'; 
    $config['remove_spaces']  = TRUE; 

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

    // Perform first file upload 
    if ($this->upload->do_upload('mp3')) 
    { 
     // Store upload file info for later use 
     $mp3_info = $this->upload->data(); 

     // Configure second file upload 
     $config['upload_path']   = 'public/doc/'; 
     $config['overwrite']   = FALSE; 
     $config['allowed_types']  = 'doc|docx'; 
     $config['max_size']    = '3000'; 
     $config['remove_spaces']  = TRUE; 
     $this->upload->initialize($config); 

     // Perform second file upload 
     // Even if this fails, the first file was still uploaded 
     if ($this->upload->do_upload('doc')) 
     { 
      // Store upload file info for later use 
      $doc_info = $this->upload->data(); 

      // Perform database interactions here 
     } 
     else 
     { 
      echo $this->upload->display_errors(); 
     } 
    } 
    else 
    { 
     echo $this->upload->display_errors(); 
    } 
} 

謝謝!

+0

我不認爲你可以確認文件上傳不會失敗,直到你真正上傳文件,因爲失敗可能是由許多不同的問題引起的。唯一想到的是,如果任何上傳失敗,則放棄所有以前上傳的文件。 – xbonez

回答

0

沒有骰子!除非您執行上傳,否則您無法驗證從php或codeigniter上傳的內容。 PHP如何驗證它尚未訪問的內容?

你必須先用javascript做一些驗證。如果所有文件都通過了js驗證,然後上傳文件並使用php/codeigniter完成驗證。