注册新用户时,用户可以上传照片或将其保留为默认值,但如果没有选择文件,则在CI中$this->upload->do_upload()
返回false。我怎样才能完全绕过这个错误。以下是我的方式:
if($this->form_validation->run()==FALSE)
{
$this->load->view('add_item_form', array('error' =>''));
}
else
{
$config['upload_path'] = 'images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$no_photo_error = "<p>You did not select a file to upload.</p>";
if(!$this->upload->do_upload() AND $this->upload->display_errors() != $no_photo_error)
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('add_item_form', $error);
}
else
{
$data['title'] = 'Add Item';
$this->load->view('success', $data);
}
}
}
答案 0 :(得分:3)
// Check if we are even trying to upload anything
if ( ! ($_FILES AND $_FILES['userfile']['name']))
{
// do_upload() logic here
}
else
{
// no files selected for upload
}
答案 1 :(得分:0)
试试这个
if($this->form_validation->run()==FALSE){
$this->load->view('add_item_form', array('error' =>''));
}
else
{
$config['upload_path'] = 'images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$no_photo_error = "<p>You did not select a file to upload.</p>";
if(!$this->upload->do_upload() AND $this->upload->display_errors() != $no_photo_error)
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('add_item_form', $error);
}elseif(empty($this->upload->data())){
//Check if the file is uploaded .
$data['title'] = 'Add Item';
$this->load->view('success', $data);
}
else
{
$data['title'] = 'Add Item';
$this->load->view('success', $data);
}
}
}