dopt85756
2017-01-21 14:52
浏览 51
已采纳

CodeIgniter使文件上传成为可选

I have found some examples of making the uploads optional but so far it hasn't work for me. I might have missed something so I'm hoping someone might catch it? This is the link to what I am following

https://blog.smalldo.gs/2013/03/optional-file-upload-field-codeigniter/

So far I am able to edit the product if I don't upload a file but if I upload a file, I get a you did not select a file to upload error. Please help. Thank you.

Controller

public function editProduct(){
      $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
      $this->form_validation->set_rules('inputproductname', 'Name', 'trim|required');
      $this->form_validation->set_rules('inputproductdescription', 'Description', 'trim|required');
      $this->form_validation->set_rules('inputproductprice', 'Price', 'trim|required');

      $inputproductname = $this->input->post('inputproductname');
      $inputproductdescription = $this->input->post('inputproductdescription');
      $inputproductprice = $this->input->post('inputproductprice');
      $inputdateadded = date('Y-m-d');
      $inputcurrentproductid = $this->input->post('inputcurrentproductid');
      $inputcurrentproductstatus = $this->input->post('inputcurrentproductstatus');

      $config['upload_path'] = $this->getProductImageFolderPath();
      $config['allowed_types'] = 'jpg|jpeg|png'; 
      $config['max_size'] = 3000;  
      $config['remove_spaces'] = TRUE;
      $config['overwrite'] = TRUE;
      $config['file_name'] = $inputproductname;
      $this->load->library('upload', $config);

      if($this->form_validation->run()==false){
            $data['product'] = $this->ProductsModel->getProduct($inputcurrentproductid);
            $data['edit'] = "true";
            $data['message']='';
            $data['inputcurrentproductid'] = $inputcurrentproductid;
            $data['inputcurrentproductstatus'] = $inputcurrentproductstatus;

            $this->load->view('control/controlMenu/navigationLink');
            $this->load->view('control/controlProducts/productDetail',$data);
            $this->load->view('control/controlMenu/navigationJquery');
        }else{
          if(isset($_FILES['upload'])&&$_FILES['upload']['size']>0){
              if(!$this->upload->do_upload()){
                  $this->session->set_flashdata('Form',$this->upload->display_errors());
                  redirect('Control/ProductDetail/'.$inputcurrentproductid."/".$inputcurrentproductstatus);
              }else{
                  $extension = $this->upload->data('file_ext');
                  $productdetails = array(
                    'name'=>$inputproductname,
                    'description'=>$inputproductdescription,
                    'price'=>$inputproductprice,
                    'imagePath'=>$config['upload_path'].$config['file_name'].$extension,
                    'dateAdded'=>$inputdateadded
                  );
                  $this->db->trans_start();
                  $this->ProductsModel->editProduct($inputcurrentproductid,$productdetails);
                  $error = $this->db->error();
                  $this->db->trans_complete();
                  if($this->db->trans_status()===false){

                  }else{
                      $this->session->set_flashdata('Form', $inputproductname . ' has been altered on the database');
                      redirect('/Control/Products');
                  }
                  if($error!=''){ 
                      $this->session->set_flashdata('Form',$error["message"]);
                      redirect('/Control/Products');
                  }
              }
          }else{
              $productdetails = array(
                  'name'=>$inputproductname,
                  'description'=>$inputproductdescription,
                  'price'=>$inputproductprice
              );
              $this->db->trans_start();
              $this->ProductsModel->editProduct($inputcurrentproductid,$productdetails);
              $error = $this->db->error();
              $this->db->trans_complete();
              if($this->db->trans_status()===false){

              }else{
                    $this->session->set_flashdata('Form', $inputproductname . ' has been altered on the database');
                    redirect('/Control/Products');
              }
              if($error!=''){ 
                    $this->session->set_flashdata('Form',$error["message"]);
                    redirect('/Control/Products');
              }  
          }
        }
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题