0

我有一个表单字段(正在上传个人图片)。所以用户从 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

4

2 回答 2

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

于 2013-04-04T16:05:23.123 回答
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');          
    }
}

}

于 2014-05-26T21:51:46.623 回答