简体   繁体   中英

Codeigniter making image upload optional in a form

I have a form with three fields- one input field, one textarea and an image upload. The form works fine for uploading image when I fill everything and select an image to upload but when I do not select an image it shows select and image. I want the form to submit even if the user does not upload an image. I am sure I had a solution yesterday for this but it stopped working today. Controller :

        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');                             
                            }
                            }
                        }
                    }
    if(isset($_FILES))
    {

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

          }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM