Php CodeIgniter使文件上传成为可选

Php CodeIgniter使文件上传成为可选,php,codeigniter,file-upload,upload,Php,Codeigniter,File Upload,Upload,我已经找到了一些使上传成为可选的例子,但到目前为止,它还没有对我起作用。我可能错过了什么,所以我希望有人能抓住它?这是我下面的链接 到目前为止,我可以编辑产品,如果我不上传文件,但如果我上传文件,我得到一个你没有选择一个文件上传错误。请帮忙。多谢各位 控制器 public function editProduct(){ $this->form_validation->set_error_delimiters('<p class="error">', '<

我已经找到了一些使上传成为可选的例子,但到目前为止,它还没有对我起作用。我可能错过了什么,所以我希望有人能抓住它?这是我下面的链接

到目前为止,我可以编辑产品,如果我不上传文件,但如果我上传文件,我得到一个你没有选择一个文件上传错误。请帮忙。多谢各位

控制器

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');
              }  
          }
        }
公共函数editProduct(){
$this->form\u validation->set\u error\u分隔符(“

”,“

”); $this->form_validation->set_规则('inputproductname','Name','trim | required'); $this->form_validation->set_规则('inputproductdescription','Description','trim | required'); $this->form_validation->set_规则('inputproductprice','Price','trim | required'); $inputproductname=$this->input->post('inputproductname'); $inputproductdescription=$this->input->post('inputproductdescription'); $inputproductprice=$this->input->post('inputproductprice'); $inputdateadded=日期('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\u name']=$inputproductname; $this->load->library('upload',$config); 如果($this->form\u 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'); }否则{ 如果(isset($\u文件['upload'])和&$\u文件['upload']['size']>0){ 如果(!$this->upload->do_upload()){ $this->session->set_flashdata('Form',$this->upload->display_errors()); 重定向('Control/ProductDetail/'.$inputcurrentproductid./'$inputcurrentproductstatus); }否则{ $extension=$this->upload->data('file_ext'); $productdetails=数组( “名称”=>$inputproductname, 'description'=>$inputproductdescription, “价格”=>$inputproductprice, 'imagePath'=>$config['upload\u path'].$config['file\u name'.$extension, “dateAdded”=>inputdateadded美元 ); $this->db->trans_start(); $this->ProductsModel->editProduct($inputcurrentproductid,$productdetails); $error=$this->db->error(); $this->db->trans_complete(); 如果($this->db->trans_status()==false){ }否则{ $this->session->set_flashdata('Form',$inputproductname.'已在数据库上更改); 重定向('/Control/Products'); } 如果($error!=''){ $this->session->set_flashdata('Form',$error[“message”]); 重定向('/Control/Products'); } } }否则{ $productdetails=数组( “名称”=>$inputproductname, 'description'=>$inputproductdescription, “价格”=>$inputproductprice ); $this->db->trans_start(); $this->ProductsModel->editProduct($inputcurrentproductid,$productdetails); $error=$this->db->error(); $this->db->trans_complete(); 如果($this->db->trans_status()==false){ }否则{ $this->session->set_flashdata('Form',$inputproductname.'已在数据库上更改); 重定向('/Control/Products'); } 如果($error!=''){ $this->session->set_flashdata('Form',$error[“message”]); 重定向('/Control/Products'); } } }
您没有告诉上载库从哪个字段获取文件

$this->upload->do_upload() // <-- no argument for this method provided
另外,请注意,在客户端,带有文件字段的表单应声明为

<form method="post" action="some_action" enctype="multipart/form-data" />

否则,文件字段也将为空

<form method="post" action="some_action" enctype="multipart/form-data" />