codeigniter从表单上传图片

时间:2013-04-04 15:47:22

标签: codeigniter codeigniter-2

我有一个我的表格字段(上传个人照片)。因此用户从pc中选择图像并提交表单。我稍后通过以下方式处理所有发布的数据:

$this->input->post()

我插入数据库的方法是:

public function add_user()
    {
        $data = array(
            'membership'=>$this->input->post('membership_type'),
            'fullname'=>$this->input->post('fullname'),
            'username'=>$this->input->post('username'),
            'password'=>md5($this->input->post('password')),
            'email'=>$this->input->post('email'),
            'city'=>$this->input->post('city'));
            'profilepic'=>$this->input->post('profilepic'));

        $this->db->insert('members',$data);

    }

现在我要在profilepic字段中插入的是服务器上图像的路径。我知道上面做错了,因为这种方式插入张贴到profilepic的图片。我需要一些纠正。是否有可以执行上传并返回路径的功能?但是,如何将图片上传与用户数据上传相关联?

此致

编辑:尝试下面提供的代码并得到了这个:

  

遇到PHP错误

     

严重性:注意

     

消息:使用未定义的常量full_path - 假设为'full_path'

     

文件名:models / membership_model.php

     

行号:29

     

遇到PHP错误

     

严重性:警告

     

消息:无法修改标头信息 - 已发送的标头   (输出始于   /home2/xsysdeve/public_html/system/core/Exceptions.php:185)

     

文件名:core / Common.php

     

行号:438

2 个答案:

答案 0 :(得分:0)

请尝试这个:

public function add_user()
    {
         $config['upload_path'] = '/file_path/';
         $config['allowed_types'] = 'gif|jpg|png|jpeg';
         $this->load->library('upload', $config);
         $this->upload->do_upload('profilepic');
         $data_upload_files = $this->upload->data();

         $image = $data_upload_files[full_path];

        $data = array(
            'membership'=>$this->input->post('membership_type'),
            'fullname'=>$this->input->post('fullname'),
            'username'=>$this->input->post('username'),
            'password'=>md5($this->input->post('password')),
            'email'=>$this->input->post('email'),
            'city'=>$this->input->post('city'));
            'profilepic'=>$image;

        $this->db->insert('members',$data);

    }

有关详情,请访问此链接:http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

答案 1 :(得分:0)

您还可以尝试使用2个CI功能并检查文本数据的POST以及图像验证:

class SomeForm extends CI_Controller{   

function add_user()  //this function is your form
{
    $this->load->view('templates/header');
    $this->load->view('new_form_entry', array('error' => ' ' ));
    $this->load->view('templates/footer');
}


function add_user_status()  //this function is your form processing
{


    $config['upload_path'] = './assets/images';
    $config['allowed_types'] = 'gif|jpg|png';
    //$config['max_size']   = '2000';
    //$config['max_width']  = '340';
    //$config['max_height']  = '190';

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

    if ( ! $this->upload->do_upload())
    {
        //check for errors with the upload
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('templates/header');
        $this->load->view('new_form_entry', $error);
        $this->load->view('templates/footer');
    }
    else
    {
        //upload the new image
        $upload_data = $this->upload->data();
        $image_name = $upload_data['file_name'];

        if($_POST){

        $data = array(
            'author'=>$_POST['author'],
            'name'=>$_POST['name'],
            'image_thumb'=>'../assets/images/'.$image_name,
            'video'=>$_POST['video'],
            'title'=>$_POST['title'],
            'body'=>$_POST['body'],
            'created' => date('Y-m-d H:i:s')

        );

        //insert the new post
        $this->your_database_model_name->insert_entry($data);
        }                   

        redirect(base_url().'SomeForm/index');          
    }
}

}