Codeigniter使图像上传在表单中可选

时间:2013-02-07 23:39:36

标签: php codeigniter

我有一个包含三个字段的表单 - 一个输入字段,一个textarea和一个图像上传。 当我填充所有内容并选择要上传的图像时,该表单适用于上传图像,但是当我不选择图像时,它会显示选择和图像。即使用户没有上传图像,我也希望表单提交。我相信我昨天有一个解决方案,但它今天停止了工作。 控制器:

        function store()
{
$this->output->enable_profiler(TRUE);

        $this->load->model('campus_m');
                    $config['upload_path'] = './uploads/';
                    $config['allowed_types'] = 'gif|jpg|png|jpeg';
                    $config['max_size'] = '100';
                    $config['max_width']  = '1024';
                    $config['max_height']  = '768';
                    $config['file_name'] = preg_replace('/[^a-z0-9]+/i','-',iconv('UTF-8','ASCII//TRANSLIT',$this->input->post('name')));
                    $config['file_name'] = trim($config['file_name'],'-').now().'.jpg';                         

                    $this->load->library('upload', $config);

                            $this->load->helper(array('form', 'url'));

                        $this->load->library('form_validation');

                        $this->form_validation->set_rules('goods', 'Goods', 'required');
                        $this->form_validation->set_rules('name', 'Name', 'required|max_length[12]');
                        if ($this->form_validation->run() == FALSE)
                        {
                                $this->load->view('campus_write_v');
                        }
                        else
                        {
                                    if (empty($_FILES['userfile'])) {
                    print_r($_FILES['userfile']);
                                if(!$query = $this->campus_m->create_review("Marla-overdoses1360186300.jpg")){
                                    $data['write_campus'] = 'The answer has not been stored.';
                                    $this->load->view('campus_write_v', $data);
                                    }
                                else {
                                    $data['write_campus'] = 'The answer has been stored. ';
                                    $this->load->view('campus_write_v', $data);
                                }   
                                    }
                                    else{
                                         if($this->upload->do_upload()){
                                if(!$query = $this->campus_m->create_review($config['file_name'])){
                                    $data['write_campus'] = 'The answer has not been stored.';
                                    $this->load->view('campus_write_v', $data);
                                    }
                                else {
                                    $data['write_campus'] = 'The answer has been stored. ';
                                    $this->load->view('campus_write_v', $data);
                                }                                               
                                    }
                            else
                            {
                                 $error = array('error' => $this->upload->display_errors());
                                 foreach ($error as $rows => $r) {
                                 echo $r ;                                 
                                 }
                                    $this->load->view('campus_write_v');                             
                            }
                            }
                        }
                    }

1 个答案:

答案 0 :(得分:0)

    if(isset($_FILES))
    {

          if(empty($_FILES['file']['name']))
          {
            //logic here    
          }else{
                if(//validation here )
                {
                   //logic here
                }

          }

    }