2012-09-07 70 views
0

我在codeigniter中有一個小應用程序。有一個表單有3個文本字段,名稱,電子郵件,位置。這足以提交。但是,所有用戶都有兩個可選字段提交圖片或視頻或兩者。我一直在工作,直到上傳的文件出現錯誤。它仍然會提交文本數據,當我希望它停止整個提交時,直到錯誤得到修復。使圖像和視頻上傳可選

這裏是我的控制器的代碼。任何幫助都會很棒。謝謝!

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Form extends MY_Controller { 

public function __construct() 
{ 
    parent::__construct(); 
    $this->load->helper(array('form', 'url')); 
} 

function index() 
    { 
     $this->load->helper(array('form', 'url')); 

     $this->load->library('form_validation'); 

     $this->form_validation->set_rules('nominee', 'Nominee', 'required'); 
     $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
     $this->form_validation->set_rules('location', 'Location', 'required'); 

     if ($this->form_validation->run() == FALSE) 
     { 
      $this->load->view('form'); 
     } 
     else 
     { 
      // Load the library - no config specified here 
       $this->load->library('upload'); 

       // Check if there was a file uploaded - there are other ways to 
       // check this such as checking the 'error' for the file - if error 
       // is 0, you are good to code 
       if (!empty($_FILES['imageupload']['name'])) 
       { 
        // Specify configuration for File 1 
        $config['upload_path'] = 'uploads/images'; 
        $config['allowed_types'] = 'gif|jpg|png'; 
        $config['max_size'] = '100'; 
        $config['max_width'] = '1024'; 
        $config['max_height'] = '768';  

        // Initialize config for File 1 
        $this->upload->initialize($config); 

        // Upload file 1 
        if ($this->upload->do_upload('imageupload')) 
        { 
         $data = $this->upload->data(); 
        } 
        else 
        { 
         echo $this->upload->display_errors(); 
        } 

       } 

       // Do we have a second file? 
       if (!empty($_FILES['videoupload']['name'])) 
       { 
        // Config for File 2 - can be completely different to file 1's config 
        // or if you want to stick with config for file 1, do nothing! 
        $config['upload_path'] = 'uploads/videos/'; 
        $config['allowed_types'] = 'gif|jpg|png'; 
        $config['max_size'] = '100'; 
        $config['max_width'] = '1024'; 
        $config['max_height'] = '768'; 

        // Initialize the new config 
        $this->upload->initialize($config); 

        // Upload the second file 
        if ($this->upload->do_upload('videoupload')) 
        { 
         $data = $this->upload->data(); 
        } 
        else 
        { 
         echo $this->upload->display_errors(); 
        } 

       } 

      $this->load->view('thankyou'); 
      //Run code to add into the database 
     } 
    } 

} 

回答

0

將所有的上傳數據邏輯放入custom validation callback

然後如果驗證失敗(包括上傳),頁面將返回到表單,並預先填入數據 - 您的用戶可以修復文件上傳。

然後你的控制器邏輯也被簡化了;

$this->load->library('form_validation'); 

    $this->form_validation->set_rules('nominee', 'Nominee', 'required'); 
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
    $this->form_validation->set_rules('location', 'Location', 'required'); 
    $this->form_validation->set_rules('imageupload', 'Image Upload', 'callback__image_upload'); 
    $this->form_validation->set_rules('videoupload', 'Video Upload', 'callback__video_upload'); 

    if ($this->form_validation->run() == FALSE) 
    { 
     $this->load->view('form'); 
    } 
    else 
    { 
     //Run code to add into the database 
     $this->load->view('thankyou'); 
    } 

function _video_upload() 
{ 
    //put upload logic here and check if its valid 
} 

function _image_upload() 
{ 
    //put upload logic here and check if its valid 
} 
+0

太好了!所以要清楚。用於上傳圖像的腳本位於_video_upload()函數中? –

+0

是的 - 你'檢查'上傳是否成功 - 如果是,返回true。如果沒有,請設置驗證錯誤消息並返回false – Laurence

+0

非常感謝您的幫助。生病嘗試現在。 –

相關問題