【发布时间】:2015-05-29 10:42:41
【问题描述】:
我在使用 Codeigniter 将 file_path 插入数据库时遇到问题,我有一个表单字段,用户可以在其中上传任何 jpg|png|gif 文件,上传的文件成功发送到上传目录并显示成功消息。但是我将如何获取上传的特定文件的 file_path 并将其插入数据库。
在我调用模型之前,下面的代码可以正常工作:
`$this->upload_model->upload_path();`
所以我的模型肯定有问题,无法弄清楚。
这是我的控制器:
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);
}
}
视图:
<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>
【问题讨论】:
标签: php mysql database codeigniter file-upload