2012-04-29 55 views
1

我想創建文件上傳,其中包含標題和上傳文件摘要的一些字段。我也看過this的問題,但我完全不理解它。CodeIgniter - 結合表單驗證和文件上傳

function someform() 
{ 
    // some variables for fields 
    ($this->form_validation->run() == FALSE) 
    { 

    } 
    else 
    { 
     // how to check if the file was uploaded? 
    } 
} 

回答

3

您應該檢查除文件上傳以外的所有其他字段的驗證。驗證成功後,檢查文件。例如:

function someform() 
{ 
    // some variables for fields 
    ($this->form_validation->run() == TRUE) 
    { 

     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '100'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 

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


     $fileTagName = 'myfile'; // e.g <input type='file' name='myfile' />  
     if ($this->upload->do_upload($fileTagName))) { 
      //Success in validation of all fields. 
     } 
     else { 
      //Failed to upload file. 
      echo $this->upload->display_errors('', ''); 
     } 
    } 
    else 
    { 
     //Other fields failed. 
    } 
} 
+0

哪些功能我要在視圖中使用 - form_open(),form_open_multipart()或兩者兼而有之? – user1257255 2012-04-29 14:15:41

+0

你應該使用'form_open_multipart'。 – 2012-04-29 14:45:02