【发布时间】:2017-10-08 08:10:06
【问题描述】:
我有一个表格来上传图像路径和其他图像数据。当我选择上传新图像时,这很有效。
如果我提交表单而不选择新图像,如何防止我的图像路径设置为空白?
这是我的控制器:
function updatewaterpurifier($id)
{
$name=$this->input->post('name');
$this->load->model('user');
//$name = $this->input->post('name');
if($this->input->post('submit'))
{
//Check whether user upload picture
if(!empty($_FILES['picture']['name'])){
$new_file_name = $this->input->post('name');
$config['upload_path'] = 'uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
$config['file_name'] = $new_file_name;
$config['max_size'] = 2048;
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
}else{
$picture = '';
}
}else{
$picture = '';
}
//Prepare array of user data
$userData = array(
'product_brand'=> $this->input->post('brand'),
'product_name' => $this->input->post('name'),
'product_price' => $this->input->post('price'),
'product_techno' => $this->input->post('technology'),
'product_des' => $this->input->post('description'),
'product_image' => $picture
);
$this->db->where('id', $id);
$this->db->update('products', $userData);
//Pass user data to model
//Storing insertion status message.
}
//Form for adding user data
$this->load->model('user');
$this->data['h']= $this->user->editpurifier($id);
$this->load->view('editwaterpurifier', $this->data, FALSE);
}
【问题讨论】:
-
只是为了指出你正在加载
$this->load->model('user');两次,为什么不将它放在控制器的 __construct 区域中,这样只需加载一次。 codeigniter.com/user_guide/general/… -
@Adi Singh:在你的代码中,如果你没有上传图像,你将存储 $picture 变量为空并将其传递给你的更新数据。因此,删除该部分即可。
-
else part ko remove kar du @Sucharitha
-
@AdiSingh:是的,该部分不是必需的,然后在您的 $userData 数组中根据条件添加 $picture 数据。
-
我通过什么我想通过我的旧图像路径
标签: php codeigniter file-upload