【问题标题】:How can i Upload image files only using code igniter?如何仅使用 codeigniter 上传图像文件?
【发布时间】:2016-07-08 04:30:13
【问题描述】:

嗨,我正在尝试使用代码点火器仅上传图像文件。我只需要上传 png、jpg、jpeg 文件。但也上传了其他类型的文件,如 .txt、.mp3 等。我的代码如下所示。请尽快帮我解决这个问题。在此先感谢大家。

    $config['upload_path'] = './avatar/';
    $config['allowed_types'] = 'gif|jpeg|png|jpg';
    $config['max_size'] = '1024mb';
    $config['max_width'] = '3000';
    $config['max_height'] = '3000';
    $this->load->library('upload', $config);

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

        $ve['data'] = $this->upload->display_errors();
    } else {
        $this->upload->data();
    } 

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
    
    $this->load->library('upload', $config);
    // Alternately you can set preferences by calling the initialize function. Useful if you auto-load the //class:
    $this->upload->initialize($config);
    
    if($this->upload->do_upload())
    {
      $data = array('upload_data' => $this->upload->data());
      $this->load->view('upload_success',$data);
    }else
    {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('file_view', $error);
    }
    

    【讨论】:

      【解决方案2】:

      我认为您需要发送 用户文件/上传的文件,同时尝试在 if 条件内对其进行验证。

      if ( ! $this->upload->do_upload('userfile'))
      {
          $error = array('error' => $this->upload->display_errors());
          $this->load->view('upload_form', $error);
      }
      else
      {
          $this->upload->data();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-21
        • 2020-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-30
        相关资源
        最近更新 更多