2011-09-22 75 views
2

我正在做一个脚本,将上传图像并保存图像路径在数据库中。我唯一的问题是如果用户没有上传图像,我想设置默认图像。设置默认图像,如果没有图像被选中上载在codeigniter

但在codeigniter中,当图像未被选中时,它会自动给出一个错误,说明没有选择输入文件。

我控制器

if (!$this->upload->do_upload('image')) 
    { 
    $error = array('error' => $this->upload->display_errors()); 
    $this->load->view('upload_success', $error); 

    } 
    else { 
     $image_data=array('image_info' => $this->upload->data('image')); 
     $image_path=$image_data['image_info']['full_path']; 

     $data =array(
     'submitedby'=>$username, 
     'city'=>$this->input->post('towncity'), 
     'image' => $image_path 

); 
    } 

可有人请建议我如何,如果用户没有显示默认的错误选择的图像设置一个默认的形象?

回答

3

在​​3210失败的条款中,检查文件是否已上传。

if (!$this->upload->do_upload('image')) { 

    if (!empty($_FILES['image']['name'])) { 
     // Name isn't empty so a file must have been selected 
     $error = array('error' => $this->upload->display_errors()); 
     $this->load->view('upload_success', $error); 
    } else { 
     // No file selected - set default image 
     $data = array(
      'submitedby' => $username, 
      'city'  => $this->input->post('towncity'), 
      'image'  => 'path/to/default/image', 
     ); 
    } 

} else { 
    $image_data = array('image_info' => $this->upload->data('image')); 
    $image_path = $image_data['image_info']['full_path']; 

    $data = array(
     'submitedby' => $username, 
     'city'  => $this->input->post('towncity'), 
     'image'  => $image_path, 
    ); 
} 

这可以进一步重构,但问题是,你可以检查$_FILES['field_name']['name'],看看是否被选中的文件。

1

如果没有找到图像,您可能无法制作所需的图像字段,然后可以在视图中设置默认图像。这样,您就不需要上传各种重复图片,只要您想要,您就可以轻松地为每个人更新默认图片。

相关问题