【问题标题】:Codeigniter upload optionalCodeigniter 上传可选
【发布时间】:2012-01-16 12:12:01
【问题描述】:

我在 Google 中查找了此内容,并在 stackoverflow 上查找了不同的答案。并且可能其中有一个很好的答案,但我仍然不明白如何在我自己的代码中实现它。

我有自己的公共函数来上传图像,但现在我希望它是可选的。此时有人需要上传文件才能通过验证,我该如何设置这个可选?

我的功能:

public function _do_upload_image()
{
    $config['upload_path'] = './company_images/';
    $config['allowed_types'] = 'jpg|jpeg|png';

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload())
    {
        $this->form_validation->set_message('_do_upload_image', $this->upload->display_errors());
    }
    else
    {
        $this->_upload_data = $this->upload->data();
    }

}

提前致谢

--编辑--

对于其他人,答案有效,当没有上传图像时,我给我的 file_name 取了另一个名称。它看起来像这样:

public function _do_upload_image()
{
    $config['upload_path'] = './company_images/';
    $config['allowed_types'] = 'jpg|jpeg|png';
    $returnArr = array();

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload())
    {
        $returnArr = array('file_name' => 'leeg.jpg');
    }
    else
    {
        $returnArr = $this->upload->data();
    }

    $this->_upload_data = $returnArr;
}

【问题讨论】:

    标签: php codeigniter upload


    【解决方案1】:

    如果您的意思是需要将上传功能设为可选,那么您可以这样做:

    public function _do_upload_image() { $config['upload_path'] = './company_images/'; $config['allowed_types'] = 'jpg|jpeg|png'; $returnArr = array(); $this->load->library('upload', $config); if ($this->upload->do_upload()) { $returnArr = $this->upload->data(); } return $returnArr; //just return the array, empty if no upload, file data if uploaded }

    希望这是有道理的

    【讨论】:

    • 这行得通,但是如果没有上传图像,我可以给 ['file_name'] 值:leeg.jpg 吗?
    • 嗯 nvm,我用我想要的所有功能解决了这个问题。我会更新我的帖子!
    【解决方案2】:

    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 可以很好地处理其他类型的错误, 希望它对你有用....

    【讨论】:

      猜你喜欢
      • 2011-03-17
      • 2017-06-06
      • 1970-01-01
      • 2017-07-19
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多