Question

Hi Every One,
I'm trying to upload the file path in data base but it is redirecting same and file is not uploading can any one help me .. This is My Controller.

<?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;
        }
    }
}

My Model Class:

<?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);
    }

}
?>

My View

<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>

Can anyone help me . Thanks.

Was it helpful?

Solution

in your _do_upload function you do not pass the argument at the function, it can't use $field_name Try to change it:

public function _do_upload($field_name)

There are different problem into your code. First of all you load the view before the validation, in this way the view is always diplaied. Try my following code (just the index function):

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
    }


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top