gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:49:18 25 4
gpt4 key购买 nike

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

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

我怎样才能做到这一点?

最佳答案

在上传表单的成功部分可以做一个像下面这样的 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);

上传 Controller
<?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

关于codeigniter - 保存图像名称并将数据发布到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30099280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com