我有插入文件名的问题所有其他字段使用codigniter正确插入以下是片段我的核心问题是如何将文件名添加到array_from_post()以插入数据库
页面控制器
public function edit($id = NULL) {
//Fetch a page or set new one
if ($id) {
$this->data['page'] = $this->page_m->get($id);
count($this->data['page']) || $this->data['errors'][] = 'Page Could not be found';
} else {
$this->data['page'] = $this->page_m->get_new();
}
$id == NULL || $this->data['page'] = $this->page_m->get($id);
//Pages for dropdown
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
//dump($this->data['pages_no_parents']);
//Setup form
$rules = $this->page_m->rules;
$this->form_validation->set_rules($rules);
//Process the form
if ($this->form_validation->run() == TRUE) {
$data= $this->uploadimage();
//dump($data);
$data = $this->page_m->array_from_post(array(
'title',
'slug',
'order',
'body',
'template',
'parent_id',
'filename'
));
$this->page_m->save($data, $id);
redirect('admin/page');
}
//Load the view
$this->data['subview'] = 'admin/page/edit';
$this->load->view('admin/_layout_main', $this->data);
}
页面控制器中的图像上传功能
public function uploadImage()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
return false;
}
else
{
$data = $this->upload->data();
return $data;
}
}
模型page_m
public function get_new() {
$page = new stdClass();
$page->title = '';
$page->slug = '';
$page->order = '';
$page->body = '';
$page->parent_id = 0;
$page->template= 'page';
$page->filename = '';
return $page;
}
function array_from_post from My_Model extending CI_Model
public function array_from_post($fields){
$data = array();
foreach ($fields as $field){
$data[$field] = $this->input->post($field);
}
return $data;
}
以下是视图中的代码段
<?php echo validation_errors(); ?>
<?php echo form_open_multipart('admin/page/edit'); ?>
<table class="table">
<tr>
<td>Image</td>
<td><?php echo form_upload($page->filename);?>></td>
</tr>
我的数据库表名称为&#34; pages&#34;和字段名称&#34;文件名&#34;按照上面的代码,它只是插入&#34; 0&#34;请建议
答案 0 :(得分:0)
试试这个
public function uploadImage()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file'))
{
return false;
}
else
{
$data = $this->upload->data();
return $data;
}
}