好的,所以我已经搞砸了几个小时,阅读文档和浏览论坛,但我不知道为什么这个上传表单不起作用。
整个表单完美运行,数据保存到数据库和所有内容。但是我只是添加了一个图像上传输入,它不起作用!我已经按照文档中的确切教程以及其他几个教程进行操作。
这是处理表单提交的代码($this->page_m
是我的模型):
public function edit ($id = NULL)
{
// Fetch a page or set a 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();
}
// Pages for dropdown
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
// Process the form
if ($this->form_validation->run('page_rules') == TRUE) {
$data = $this->page_m->array_from_post(array(
'title',
'slug',
'body',
'template',
'parent_id'
));
if(!empty($_FILES['userfile'])) {
$this->do_upload();
}
$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);
}
这是处理照片上传的代码,在$this->form_validation->run()
检查后立即调用:
//upload a slider photo
public function do_upload() {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => site_url('uploads/')
);
$this->load->library('upload');
$this->upload->initialize($config);
$field_name = "userfile";
if ( ! $this->upload->do_upload($field_name)) {
$this->data['error'] = array('error' => $this->upload->display_errors());
$this->data['subview'] = 'admin/page/edit';
$this->load->view('admin/_layout_main', $this->data);
} else {
return true;
}
}
为了调试目的,我特意使这个上传脚本尽可能简单和基本,但我仍然没有成功让这个表单正确上传。
完成上传后,我需要将图像名称保存在我的数据库表中,但这不是重点。我只需要让这个实际上传。
已解决——上传目录无效