gpt4 book ai didi

php - 如何使用 codeigniter 将 file_path 插入数据库

转载 作者:行者123 更新时间:2023-11-30 22:47:12 24 4
gpt4 key购买 nike

我在使用 Codeigniter 将 file_path 插入数据库时​​遇到问题,我有一个表单字段,用户可以在其中上传任何 jpg|png|gif 文件,上传的文件已成功发送到上传目录并显示成功消息。但是我如何获取上传的特定文件的 file_path 并将其插入数据库。

在我调用模型之前,下面的代码工作正常:

`$this->upload_model->upload_path();`

所以肯定是我的模型有问题,想不通。

这是我的 Controller :

class Upload extends CI_Controller {

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

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

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

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

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

$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());

// when the code below is uncommented it shows error,otherwise runs
// $this->upload_model->upload_path();

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

}
}
}

这是我的模型(显示错误):

class Upload_model extends CI_Model {

// this function will be called instantenneously

public function __construct()
{
parent::__construct();

$this->load->database();
}
public function upload_path(){

// ::::::::UNDER CONSTRUCTION::::::::::

$data = array(
'path' =>$upload_data['full_path'],

);

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


}

}

View :

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

<?php echo $error;?>

<form method="POST" action="/sandeep/ci/index.php/upload/do_upload" enctype="multipart/form-data" />

<input type="file" name="userfile" size="20" />

<br /><br />

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

</form>

</body>
</html>

最佳答案

用你需要上传到你的数据库中的任何东西创建一个数组...然后将它插入到数据库中把这样的东西放在其他的下面

//create array to load to database
$image_data = $this->upload->data();
$insert_data = array(
'name' => $image_data['file_name'],
'path' => $image_data['full_path'],
'thumb_path'=> $image_data['file_path'] . 'thumbs/'. $image_data['file_name'],
'tag' => $tag
);

$this->db->insert('photos', $insert_data);//load array to database

关于php - 如何使用 codeigniter 将 file_path 插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29261128/

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