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();
}
任何錯誤,也許? – Ven
一個PHP錯誤遇到 嚴重性:注意 消息:數組字符串轉換 文件名的:mysql/mysql_driver.php 行號:535 –
是什麼'$這個 - > upload->返回的數據( )'? – rcpayan