Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Codeigniter 保存图像名称并将数据发布到数据库中_Codeigniter - Fatal编程技术网

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

Codeigniter 保存图像名称并将数据发布到数据库中,codeigniter,Codeigniter,我正在做一个志愿者必须填写登记表的项目 我希望志愿者能够上传个人资料图像,并将图像名称保存到数据库,以及在同一表单上发布数据 我怎样才能做到这一点呢?在上传表单的成功部分,我可以使用下面的if语句进行更新 更新 $user_info = $this->get_user(); if ($user_info) { $image_info = $this->upload->data(); $data = array( 'username' => $user_info['u

我正在做一个志愿者必须填写登记表的项目

我希望志愿者能够上传个人资料图像,并将图像名称保存到数据库,以及在同一表单上发布数据


我怎样才能做到这一点呢?

在上传表单的成功部分,我可以使用下面的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>

上传表单


上载库

编码点火器2


Codeigniter 3你的问题不清楚

同时提供、查看和控制器。 无论如何,看看这篇文章,它会帮助你的

使用
在处理图像和文本数据时在视图文件中

在控制器中,最佳实践是定义两个函数,一个用于图像上传,另一个用于验证和过滤数据

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

            }

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

您的代码在哪里?您尝试了什么?您希望上载并存储在数据库中??将jpeg文件的名称存储在数据库中,并将图像保存到某个位置(至少在codeigniter 3中)
此->上载数据()
应该是
这个->上传->数据()@mheavers只是一个输入错误