保存图像名称并将数据发布到数据库中

时间:2015-05-07 11:05:34

标签: codeigniter

我正在开展一个志愿者必须填写注册表格的项目。

我希望志愿者能够上传个人资料图片并将图片名称保存到数据库以及同一表格上的帖子数据。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:4)

在上传表单的成功部分可以执行如下所示的if语句以进行更新

<强>更新

$user_info = $this->get_user();

if ($user_info) {

$image_info = $this->upload->data();

$data = array(
'username' => $user_info['username'],
'image' => $image_info['file_name']
);

// Only updates password if post value true
if(isset($_POST['password'])) {
   $data = array(
      'password' => $this->input->post('password');
   );
}


$this->db->where('id', $user_info['id']);
$this->db->update('tablename', $data);

}

否则在上传表单的成功部分可以执行如下插入

插入

$image_info = $this->upload->data();

$data = array(
   'username' => $this->input->post('username'),
   'password' => $this->input->post('password')
   'image' => $image_info['file_name']
);


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

上传控制器

<?php

class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '0'; // Unlimited
        $config['max_width']  = '0'; // Unlimited
        $config['max_height']  = '0'; // Unlimited

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

        // Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
        $this->upload->initialize($config);

        $input_name = "userfile";

        if ( ! $this->upload->do_upload($input_name))
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {


            $user_info = $this->get_user();

            if ($user_info) {

                $image_info = $this->upload_data();

                $data = array(
                    'username' => $user_info['username'],
                    'image' => $image_info['file_name']
                );

                $this->db->where('id', $user_info['id']);
                $this->db->update('tablename', $data);

            }

            $this->load->view('upload_success', $data);
        }
    }

    public function get_user() {

        // your user model data

    }
}
?>

查看

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('upload/do_upload');?>
<input type="text" name="name"/>
<input type="password" name="password"/>
<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>

上传图书馆

Codeigniter 2 http://www.codeigniter.com/userguide2/libraries/file_uploading.html

Codeigniter 3 http://www.codeigniter.com/user_guide/libraries/file_uploading.html

答案 1 :(得分:3)

你的问题不明确,

提供,查看,控制器。 无论如何看到这篇文章它会帮助你。

click here

使用<?php echo form_open_multipart('yourcontroler/add');?> 处理图像和文本数据时在视图文件中。

在控制器中,最佳做法是为图像上传定义两个函数,另一个用于验证和过滤数据。喜欢

             public function do_upload() {
                $path = '_assets/images/';
                $fileName = 'userpic.png';
                $config = array(
                    'allowed_types' => 'jpg|png|gif',
                    'upload_path' => $path,
                    'max_size' => '200',
                    'overwrite' => true,
                    'file_name' => $fileName,
                    'max_height' => "300",
                    'max_width' => "300",

                );

                $this->load->library('upload', $config);
                if (!$this->upload->do_upload('logo')) {
                    echo "error ";
                    die;
                } else {
                    $imageData = $this->upload->data();
                }

            }

对于数据存储使用简单的功能,您通常使用表单输入。