文件上传路径到codeigniter中的数据库

时间:2012-06-12 05:51:43

标签: 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>

任何人都可以帮助我。 感谢。

1 个答案:

答案 0 :(得分: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
    }


}