【问题标题】:codeigniter file upload - optional?codeigniter 文件上传 - 可选?
【发布时间】:2011-03-17 23:16:46
【问题描述】:

我确信这很简单,但我看不出如何使用 CI 上传文件。

如果将文件输入框留空,则会出现“您没有选择上传文件”的错误。

我希望它是可选的原因是我的表单编辑了目录类型列表,并且我不需要每次编辑列表时都上传图像。

有没有办法删除文件类上的“必需”错误处理

【问题讨论】:

    标签: file codeigniter upload


    【解决方案1】:

    使用此代码:-

    $config['upload_path'] = 'assets/img/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $this->load->library('upload', $config);
        // Upload the file
            if ($this->upload->do_upload('Image')){
                $dataimage = $this->upload->data();
                    $data = array(
                       'image' => $dataimage['file_name'],
                       'UserName' => $this->input->post('UserName'),
                       'Password' => $this->input->post('Password'),
                       'xid' => $this->input->post('xid')
                    );
            }
            else{
                   /*$out['msg'] = show_err_msg($this->upload->display_errors());
                   echo json_encode($out);
                   exit();*/
                    $data = array(
                       'image' => NULL,
                       'UserName' => $this->input->post('UserName'),
                       'Password' => $this->input->post('Password'),
                       'xid' => $this->input->post('xid')
                    );
            }
    

    【讨论】:

      【解决方案2】:

      在调用 do_upload() 之前在控制器中使用此代码

      if (is_uploaded_file($_FILES['field_name']['tmp_name'])) {
          // your code here
      }
      

      【讨论】:

        【解决方案3】:

        codeigniter 文件上传可选......完美...... :)

        --------- 控制器 ---------

        function file()
        {
         $this->load->view('includes/template', $data);
        }
        
        function valid_file()
        {
         $this->form_validation->set_rules('userfile', 'File', 'trim|xss_clean');
        
         if ($this->form_validation->run()==FALSE) 
         {
            $this->file();
         }
         else
         {
          $config['upload_path']   = './documents/';
          $config['allowed_types'] = 'gif|jpg|png|docx|doc|txt|rtf';
          $config['max_size']      = '1000';
          $config['max_width']     = '1024';
          $config['max_height']    = '768';
        
          $this->load->library('upload', $config);
        
          if ( !$this->upload->do_upload('userfile',FALSE))
          {
            $this->form_validation->set_message('checkdoc', $data['error'] = $this->upload->display_errors());
        
            if($_FILES['userfile']['error'] != 4)
            {
                return false;
            }
        
          }
          else
          {
            return true;
          }
        }
        

        我只是使用这行使其成为可选,

        if($_FILES['userfile']['error'] != 4)
        {
         return false;
        }
        
        $_FILES['userfile']['error'] != 4 is for file required to upload.
        

        您可以使用 $_FILES['userfile']['error'] != 4 使其变得不必要,然后它将传递此错误以获取所需文件和 使用 return false 可以很好地处理其他类型的错误, 希望它对你有用....

        【讨论】:

        • 你能解释一下4的意义以及你从哪里得到这个吗?
        • $phpFileUploadErrors = array( 0 => '没有错误,文件上传成功', 1 => '上传的文件超过了php.ini中的upload_max_filesize指令', 2 => '上传的文件超过了 HTML 表单中指定的 MAX_FILE_SIZE 指令', 3 => '上传的文件只是部分上传', 4 => '没有上传文件', 6 => '缺少临时文件夹', 7 => '无法将文件写入磁盘。', 8 => 'PHP 扩展停止了文件上传。', );来自:php.net/manual/en/features.file-upload.errors.php
        【解决方案4】:

        使用以下内容:

        <?php if ( $_FILES AND $_FILES['field_name']['name'] ) 
        {
            // Upload the file
        }
        

        【讨论】:

        • 在哪里使用?为什么要多输入一点来解释它的去向如此困难......
        • 我确信在控制器中,在调用 codeigniters 上传 rutine 之前。
        猜你喜欢
        • 2017-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-19
        • 2011-11-06
        • 1970-01-01
        相关资源
        最近更新 更多