0

注册新用户时,用户可以上传照片或将其保留为默认值,但在 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);
                }
            }
        }
4

2 回答 2

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
}
于 2012-12-12T16:52:44.833 回答
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);
            }
        }
    }
于 2012-12-13T06:39:27.033 回答