2013-01-24 168 views
7

我嘗試寫代碼,我可以提交一個表單在數據庫中輸入內容,同時進行文件上傳,並將它存儲在一個文件夾在我的服務器裏笨表格文件提交上傳

文件夾的位置被稱爲其位於我的網站

這裏的根上傳是我的代碼

控制器(site.php)

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

class Site extends CI_controller { 

public function __construct() 
{ 
    parent::__construct(); 
    // Your own constructor code 
    $this->load->model("site_model"); 
    $this->load->helper(array('form', 'url')); 
} 

public function cmain($type,$page) 
{ 
    $data = $this->_initialize_data(); //this is just a bunch of variables that im calling in another function 
    $data['type'] = $type; 
    $data['page'] = $page; 
    $this->load->vars($data); 
    $this->load->view('site/cmain'); 
} 


public function m1() 
     {    

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

      if(isset($_POST['m1'])) 
      { 
       $suffix = $this->input->post("suffix"); 
       $fn = $this->input->post("fn"); 
       $mn = $this->input->post("mn"); 
       $ln = $this->input->post("ln"); 


       $newdata = array('suffix'=>$suffix, 
           'fn'=>$fn, 
           'mn'=>$mn, 
           'ln'=>$ln, 

           ); 
       //this code is for the file upload 
          $config['upload_path'] = 'uploads'; 
          $config['allowed_types'] = '*'; 

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

          $data = array('upload_data' => $this->upload->data()); 
       //end of file upload codes     

       $this->db->insert('myself', $newdata); 

       redirect(base_url() . "site/complaint");  

      } 

     } 

視圖(CMAIN .PHP)

<form action="<?php echo base_url();?>site/m1" name="details" id="details" method="post" enctype="multipart/form-data"> 
     <table class='table' width="100%"> 
      <tr> 
      <td colspan='2'> 
       <b><font color="#3B608C">Personal Information</font></b> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Suffix (e.g. Jr., III) 
      </td> 
      <td> 
       <input type="text" name="suffix" id="suffix" value=""> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       First Name* 
      </td> 
      <td> 
       <input type="text" name="fn" id="fn" value=""> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Middle Name* 
      </td> 
      <td> 
       <input type="text" name="mn" id="mn" value=""> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Last Name* 
      </td> 
      <td> 
       <input type="text" name="ln" id="ln" value=""> 
      </td> 
     </tr> 
</table> 

      <table> 
     <tr> 
     <td width="50%"> 
     Please attach documents pertinent to these complaints. <br> 
     (Attach a zip file if more than one document)<br> 
     </td> 
     <td align="center"> 
     <input name = "userfile" type="file" class="input-xlarge" id = "userfile" /> 
     </td></tr> 
     </table> 
     <input type="submit" value="Submit Form" class="pull-right" id="submit" name="m1"/> 
</form> 

形式帖子正確像後綴,FN,LN和MN 但是文件上傳心不是工作

我盡力遵循什麼笨文檔樣本,只有數據庫得到了線我想我需要

我做錯了什麼?

感謝

+1

嘗試使用valida重刑班。然後將圖片上傳設置爲自定義回調。 –

回答

4

終於得到它的工作

我用它進行文件上傳部分

   //start of file upload code 
       $config['upload_path'] = './uploads/'; 
       $config['allowed_types'] = '*'; 
       $this->load->library('upload', $config); 
       $this->upload->do_upload(); 
       $data = $this->upload->data(); 
       //end of file upload code 

,並改變了

<form action="<?php echo base_url();?>site/m1" name="details" id="details" method="post" enctype="multipart/form-data"> 

<?php echo form_open_multipart('site/c_upload');?> 
+0

什麼是c_upload?我在控制器中看不到名爲c_upload的函數 –

0
  • 您是否爲'uploads'文件夾設置了寫入權限? (例如:php.net/manual/en/function.chmod.php)

  • 總是做測試驅動開發。我下面的代碼塊從this url

    if (! $this->upload->do_upload()) 
    { 
        $error = array('error' => $this->upload->display_errors()); 
    
        $this->load->view('upload_form', $error); 
    } 
    
1

我認爲你必須設置文件上傳偏好

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

表單輔助

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

For further detail have a look