桌子
My table(posts){id, title, post, date_added, userID, active, urlfile}
在这里,我想用我的这个控制器和模型上传一个带有视图的文件,它必须将该文件名插入到 urlfile 并将其上传到(/uploads/
)。如果我只插入文本并删除上传部分,我的控制器工作正常,但它不工作
控制器代码:
function new_post() {
$data['errors'] = 0;
if ($_POST) {
$config = array( array('field' => 'title', 'rules' => 'trim|requird'), array('field' => 'post', 'rules' => 'trim|required'));
$this -> load -> library('form_validation');
$this -> form_validation -> set_rules($config);
if ($this -> form_validation -> run() == false) {
$data['errors'] = validation_errors();
}
$data = array('title' => $_POST['title'], 'post' => $_POST['post'], 'active' => $_POST['active']);
$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);
$this -> upload -> do_upload();
$data = array('upload_data' => $this->upload->data());
$this -> post_m -> insert_post($data);
redirect(base_url() . 'posts/index_admin');
} else {
$this -> load -> view('admin/post_new', $data);
}
}
查看代码
最新帖子:
<?php if ($errors){ ?>
<?php echo $errors ?>
<?php } ?>
<form action="<?php echo base_url()?>posts/new_post" method="post">
<p><?php echo form_textarea('title'); ?></p>
<p><?php echo form_textarea('post'); ?></p>
<input type="file" name="userfile" size="20" />
<p>Status: <select name="active">
<option value="1">Active</<option>
<option value="0">Un Active</<option>
</select>
</p>
<p><input type="submit" value="add post" /></p>
</form>
型号代码:
function insert_post($data){
$this->db->insert('posts', $data);
return $this->db->insert_id();
}