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条回答 默认 最新

  • dragon8002 2017-01-21 18:23
    关注

    You didn't tell the upload library from which field to take the file.

    $this->upload->do_upload() // <-- no argument for this method provided
    

    If no field name is provided, by default it tries to get the file from userfile field [docs], but your file input is called upload as seen in the code sample. So you should do

    if(!$this->upload->do_upload("upload")){
    

    Also, note that on the client side the form with file field should be declared as

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

    otherwise file field will be empty too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog