2012-06-12 3 views
-1

Привет каждый,
Я пытаюсь загрузить путь к файлу в базе данных, но он перенаправляет то же самое, и файл не загружается, может ли кто-нибудь мне помочь .. Это мой контроллер ,путь загрузки файла в базу данных в codeigniter

<?php 
if (!defined ('BASEPATH'))exit('no direct scripts allowed '); 
class Main extends CI_Controller 
{ 
    function __construct() 
    { 
     parent::__construct(); 
     $this->load->library('session'); 
    } 



    function insert_quiz_data() 
    { 

    $this->load->library('form_validation'); //loading validation library 

    $this->form_validation->set_rules('qtitle','quiz_title'); //setting rules for validation 
    $this->form_validation->set_rules('qcategory1','quiz_category1'); 
    $this->form_validation->set_rules('qcategory2','quiz_category2'); 
    $this->form_validation->set_rules('qcategory3','quiz_category3'); 
    $this->form_validation->set_rules('astatus','email'); 

    $this->form_validation->set_rules('userfile', 'quiz_image_path', 'callback__do_upload'); //userfile is the upload file field name in form , 
    echo 'testing'; 

    if($this->form_validation->run()!=false) 
    { 


     //Get the info from the form 
     $username = $this->input->post('qtitle'); 
     $password = $this->input->post('qcategory1'); 
     $passconf = $this->input->post('qcategory2'); 
     $date  = $this->input->post('qcategory3'); 
     $email  = $this->input->post('astatus'); 


     $temp=$this->upload->data(); 
     $image=$temp['file_name'];// to get image file name rom upload script , as it could be stored in the databae 


     //load the model 
     $this->load->model('quiz_mode'); 

     //execute the registration query 
     $data = $this->quiz_mode->register_user($username,$password,$passconf,$date,$image,$email); 

     //if $data == TRUE 
     if($data) 
     { 
      echo "you have successfully made your registration"; 
      $this->load->view('logged_in_area'); 
     } 
     else 
     { 
      echo "faillled"; 
     } 
    } 
    else{ 


     //You arrived here just in case the form fields are not correct 
     $this->load->view('logged_in_area'); //loading reg form here 
    } 
} 
    public function _do_upload() 
    { 
     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '100'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 
     $field_name = 'userfile'; 
     $this->load->library('upload', $config); 
     if (! $this->upload->do_upload($field_name)) 
     { 
       $this->form_validation->set_message('_do_upload', $this->upload->display_errors()); 
       return FALSE; 
     } 
     else 
     { 
      $image_data = $this->upload->data(); 
      $filename = $image_data['file_name']; 
      $config = array 
      (
       'source_image' => $image_data['full_path'], 
       'new_image' => './uploads/thumbs/', 
       'maintain_ratio' => false, 
       'width' => 300, 
       'height' => 200 
      ); 
      $this->load->library('image_lib', $config); 
      $this->image_lib->resize(); 
      return TRUE; 
     } 
    } 
} 

Моя модель Класс:

<?php 
class File_model extends CI_Model 
{ 
    function File_model() 
    { 
     parent::__construct(); 
    } 
    function register_user($username,$password,$passconf,$date,$image,$email) 
    { 

      $data= array('username'=>$username, 
     'password'=>$password, 
     'passconf'=>$passconf, 
     'date'=>$date, 
     'image'=>$image, 
     'email'=>$email 
     ); 
     $res= $this->db->insert('register', $data); //register is my table name 
     return $res; 
     var_dump($res); 
    } 

} 
?> 

Мой Посмотреть

<html> 
<body> 

<?php $this->load->helper('form'); ?> 
<?php echo form_open_multipart('index.php/main'); ?> 
<?php echo validation_errors(); ?> 

<table width="348" align="center" cellpadding="0" cellspacing="0"> 
    <tr> 
     <td colspan="2" align="center"><h2><strong>REGISTERATION AREA</strong></h2></td> 
    </tr> 
    <tr><?php //echo validation_errors(); ?> 
     <td width="97" height="40"><label for"username"> <strong>Username </strong></label></td> 
     <td width="249"><input type="text" id="username" name="username" value="<?php echo set_value('username'); ?>"/></td> 
    </tr> 
    <tr> 
     <td><label for"password"> <strong>Password </strong></label></td> 
     <td><input type="password" name="password" id="password" value="<?php echo set_value('password'); ?>"/></td> 
    </tr> 
    <tr> 
     <td><label for"confirm password"> <strong>Passconf</strong></label></td> 
     <td><input type="password" name="passconf" id="passconf" value="<?php echo set_value('passconf'); ?>"/></td> 
    </tr> 
    <tr> 
     <td><label for"date "><strong>Date</strong></label></td> 
     <td><input type="text" name="datepicker" id="datepicker" /></td> 
    </tr> 
    <tr> 
     <td><strong>Upload Picture</strong></td> 
     <td><input type="file" name="userfile" size="20" id="userfile" /></td> 
    </tr> 
    <tr> 
     <td><label for"email"> <strong>Email</strong></label></td> 
     <td><input type="text" name="email" value="<?php echo set_value('email'); ?>"/></td> 
    </tr> 
    <tr> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
    </tr> 
    <tr> 
     <td height="44">&nbsp;</td> 
     <td align="center"> 
     <input type="submit" id="submit" value="Register" /></td> 
    </tr> 
</table> 

</body> 
</html> 

Может кто-нибудь мне помочь. Спасибо.

+0

вы ** действительно ** не должны делать файл uplaod с помощью проверки поля ввода ... тот неправильный путь об этом, должен загружать файл только один раз, все проверки правильны, а не когда вы проверяете достоверность файла. – Jakub

+0

@Jakub Ok Можете ли вы дать решение ... – Naveen

ответ

1

в вашей _do_upload функции вы не передаете аргумент в функции, он не может использовать $field_name Попробуйте изменить его:

public function _do_upload($field_name) 

Есть другая проблема в вашем коде. Прежде всего, вы загружаете представление перед валидацией, таким образом, вид всегда diplaied. Попробуйте мой следующий код (только функция индекса):

function index() 
{ 


    $this->load->library('form_validation'); //loading validation library 

    $this->form_validation->set_rules('username','username','Rquired'); //setting rules for validation 
    $this->form_validation->set_rules('password','password','required|md5|trim'); 
    $this->form_validation->set_rules('passconf','password confirmation','required|md5|trim'); 
    $this->form_validation->set_rules('datepicker','date','required'); 
    $this->form_validation->set_rules('email','email','required'); 

    $this->form_validation->set_rules('userfile', 'Image', 'callback__do_upload'); //userfile is the upload file field name in form , 


    if($this->form_validation->run()!=false) 
    { 


     //Get the info from the form 
     $username = $this->input->post('username'); 
     $password = $this->input->post('password'); 
     $passconf = $this->input->post('passconf'); 
     $date  = $this->input->post('datepicker'); 
     $email  = $this->input->post('email'); 


     $temp=$this->upload->data(); 
     $image=$temp['file_name'];// to get image file name rom upload script , as it could be stored in the databae 


     //load the model 
     $this->load->model('file_model'); 

     //execute the registration query 
     $data = $this->file_model->register_user($username,$password,$passconf,$date,$image,$email); 

     //if $data == TRUE 
     if($data) 
     {    
      echo "you have successfully made your registration"; 
     } 
     else 
     { 
      echo "faillled"; 
     } 
    } 
    else{ 
     //You arrived here just in case the form fields are not correct 
     $this->load->view('register_form'); //loading reg form here 
    } 


} 
+0

Я пытался, но не получал. Можете ли вы дать ясно. – Naveen

+0

см. Мой ответ на вопрос –

+0

Thaks, он работает, но когда я вставляю этот код в какой-то другой метод, он запускает else { $ this-> load-> view ('register_form'); } Вы можете мне помочь. – Naveen