【发布时间】:2015-08-18 19:33:34
【问题描述】:
我有一个上传图片的功能,可以很好地将图片名称插入数据库。我创建了一个名为edit() 的新函数,其代码完全相同,允许用户编辑表单并上传图像。此图像名称将更新数据库中现有的图像名称。
但是,每次用户编辑表单并选择新图像时,它不会保存新的图像名称,而是从数据库中擦除以前的名称并且什么都不显示。
我的控制器:
public function edit(){
$this->form_validation->set_rules('title', 'Title', 'required|xss_clean|trim|is_unique[news.title]');
$this->form_validation->set_rules('description', 'description', 'required|xss_clean|trim');
$this->form_validation->set_rules('notes', 'notes', 'required|xss_clean|trim');
if ($this->form_validation->run() == FALSE)
{
$data['error'] = '';
$data['page_title']="Edit News";
$this->load->view("edit_news_form",$data);
}
else {
$new = array(
'upload_path' => "./images/news",
'allowed_types' => "gif|jpg|png|jpeg|JPG",
'overwrite' =>False,
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $new);
$this->load->helper(array('form', 'url'));
if(isset($_FILES)) {
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload>data());
$this->load->view('manage_news',$data);
}
else {
if(empty($_FILES['userfile']['name'])){ $imagename ='';}
else {
$message2 = "Please choose another file type. gif,jpg,png,jpeg ";
echo "<script type='text/javascript'>alert('$message2');</script>";
redirect('resetPasswordController/edit_news_form', 'refresh');
}
}
}
$this->db->trans_start();
$imagename = $this->upload->data();
$data = array(
'image'=>$imagename['file_name'],
);
$this->users_model->update($id,$data);
$this->db->trans_complete();
$message2 = "The selected news has been edited";
echo "<script type='text/javascript'>alert('$message2');</script>";
redirect('resetPasswordController/manage_news', 'refresh');
}
}
我的看法:
<?php echo form_open_multipart('resetPasswordController/news');?>
<label>Image</label>
<input name = "userfile" type="file" />
<label> <input type="submit" name="submit" value="Save and Continue">
</label>
我的模特:
function update($id,$data){
$this->db->where('id', $id);
$this->db->update('news',$data);
}
我的模型函数正在工作,因为它将数据库更新为空。我认为问题出在视图和控制器上。
当我输入var_dump($_FILES) 时,它返回一个空数组,这意味着没有选择文件。
我该如何解决这个问题?
【问题讨论】:
-
你关闭表单标签了吗???
-
是的,我有。该表单正在工作,因为我在另一个上传情况下使用了确切的表单代码。这是我的控制器的逻辑问题。它没有读取用户在表单中选择的文件。
标签: php forms file codeigniter upload