2015-07-13 187 views
0

我想使用CodeIgniter將圖像上傳到數據庫我使用以下代碼。現在它顯示錯誤「您沒有選擇要上傳的文件」。即使我選擇了。任何人都可以說我的代碼有什麼錯誤。我無法使用codeigniter上傳圖像

<?php 


class Register_form_controller extends CI_Controller 
{ 
    function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper(array('form','url')); 
     $this->load->library('form_validation','validation'); 
    } 

    function index() 
    { 
     $this->form_validation->set_rules('name','name','required'); 
     $this->form_validation->set_rules('age','age','required'); 
     $this->form_validation->set_rules('gender','gender','required'); 
     $this->form_validation->set_rules('mobilno','mobilno','required'); 
     $this->form_validation->set_rules('email','email','required|valid_email|is_unique[register.email]'); 
     $this->form_validation->set_rules('uname','username','required'); 
     $this->form_validation->set_rules('password','password','required|matches[cpassword]'); 
     $this->form_validation->set_message('cpassword','password mismatched','required'); 
     $this->form_validation->set_rules('cpassword','cpassword','required'); 
     if($this->form_validation->run() == FALSE) 
     { 
      $this->load->view("Register_form_view"); 

     } 
     else 
     { 
      $this->doregister(); 
     } 
    } 
    function doregister() 
    { 
     if($this->input->post("submit")) 
     {   
      $config = array( "upload_path" => "image/", 
           "allowed_types" => "jpg|gif|jpeg|png", 
           "max_size"  => "2048", 
           "max_width"  => "", 
           "max_height" => "" 
           ); 

      $this->load->library("upload",$config); 
      if($this->upload->do_upload('image')) 
      { 
       $upload_data = $this->upload->data(); //echo"<pre>"; print_r($upload_data); exit; 

      } 
      else 
      { 

       echo $error = $this->upload->display_errors(); //echo"<pre>"; print_r($error); exit; 
      } 

      $name = $this->input->post('name'); 
      $age = $this->input->post('age'); 
      $gender = $this->input->post('gender'); 
      $mobno = $this->input->post('mobilno'); 
      $email = $this->input->post('email'); 
      $uname = $this->input->post('uname'); 
      $pass = $this->input->post('password'); 
      $cpass = $this->input->post('cpassword'); 
      $image = $upload_data['file_name']; 
      $data = array( 'name'  => $name, 
          'age'  => $age, 
          'gender' => $gender, 
          'mobilno' => $mobno, 
          'email'  => $email, 
          'uname'  => $uname, 
          'password' => $pass, 
          'image'  => $image 
      ); 
      //echo"<pre>"; print_r($data); exit; 
      //$this->load->library('form_validation'); 
      $this->load->model("Register_model","register"); 

      if($this->register->insertData($data)) 
      { 
       $this->load->library("email"); 
       $this->email->from('[email protected]'); 
       $this->email->to($email); 
       $this->email->subject('Confirmation message'); 
       $message = "you Registred successfully \n"; 
       $message = "Thank you for joining"; 
       $this->email->message($message); 
       if($this->email->send()) 
       { 
        echo 'success'; 
        echo"Registred successfully"; 
       } 
       else 
       { 
        echo "mail has send failed"; 
        echo" but you joined the group"; 
       } 
       //redirect('Register_form_controller/index'); 
      } 
     } 
     else 
     { 
      echo"registration failed"; 
     } 
    } 
} 
?> 
+0

將圖像路徑放置應用程序文件夾? –

+0

[圖片上傳不能在codeignitor中工作]的可能的重複(http://stackoverflow.com/questions/29698048/image-upload-not-working-in-codeignitor) –

+0

你面臨什麼問題? –

回答

0
 function doregister() 
     { 
     if($this->input->post("submit")) 
     {   
      $config['upload_path'] = './image/'; 
      $config['allowed_types'] = 'jpg|gif|jpeg|png'; 
      $config['max_size'] = '2048'; 
      $config['max_width'] = '1024'; 
      $config['max_height'] = '768'; 
      $this->load->library('upload', $config); 

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

      $name = $this->input->post('name'); 
      $age = $this->input->post('age'); 
      $gender = $this->input->post('gender'); 
      $mobno = $this->input->post('mobilno'); 
      $email = $this->input->post('email'); 
      $uname = $this->input->post('uname'); 
      $pass = md5($this->input->post('npassword'));// important config md5 password 
      $cpass = md5($this->input->post('cpassword'))// important config md5 password; 
      $file= array( 'name'  => $name, 
          'age'  => $age, 
          'gender' => $gender, 
          'mobilno' => $mobno, 
          'email'  => $email, 
          'uname'  => $uname, 
          'password' => $pass, 
          'image'  => $data['file_name'] 
          ); 

       $this->load->model("Register_model","register"); 

       if($this->register->insertData($file)) 
       { 
        $this->load->library("email"); 
        $this->email->from('[email protected]'); 
        $this->email->to($email); 
        $this->email->subject('Confirmation message'); 
        $message = "you Registred successfully \n"; 
        $message = "Thank you for joining"; 
        $this->email->message($message); 
        if($this->email->send()) 
        { 
         echo 'success'; 
         echo"Registred successfully"; 
          redirect('your_page'); 
        } 
        else 
        { 
         echo "mail has send failed"; 
         echo" but you joined the group"; 
        } 
       } 
      } 

      } else { 
       echo"registration failed"; 
      } 
     } 
+0

請在您的代碼中添加一些說明。 –

+0

謝謝你的回答,但我收到同樣的錯誤「你沒有選擇一個文件上傳..」 – mageshkumar

+0

我編碼只是使用CI簡單的註冊表單。我想添加圖像就像選擇配置文件圖像。 – mageshkumar

0

嘿,我得到了同樣的錯誤多的時間我所有的PHP代碼永遠是正確的,但我總是忘了enctype="multipart/form-data"在表單標籤檢查你在表單中添加此

<form action="demo_post_enctype.asp" method="post" enctype="multipart/form-data"> 
    First name: <input type="text" name="fname"><br> 
    Last name: <input type="text" name="lname"><br> 
    <input type="submit" value="Submit"> 
</form> 

如果不要'在您的表單標籤中添加enctype="multipart/form-data" codeigniter始終抱怨You did not select a file to upload有關在代碼選項中上傳圖像的詳細信息,請檢查此link

+0

是的,我用,但我很抱歉,我已經打開另一個窗體使用form_open只有我得到那個錯誤。謝謝你,我清除了那個錯誤。 – mageshkumar