【问题标题】:upload size limit in codeigniter在codeigniter中上传大小限制
【发布时间】:2013-04-25 06:44:30
【问题描述】:

我有一个用户可以注册的页面。他在此过程中上传了个人资料图片。我想限制大小,但除了 $config['maxsize'] 之外,没有过多强调 codeigniter 文档。我尝试了以下操作,但没有收到任何消息。我将大小设置为 10 (KB) 只是为了测试。我是否必须以某种方式处理它才能将消息传达给我的视图?

public function add_user()
    {


        $config['upload_path']   = './uploads/';        
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '10';
        //$config['max_width'] = '1024';
        //$config['max_height'] = '768';

        $this->load->library('upload', $config);
        $fullImagePath;
        if (isset($_FILES['userfile']) && !empty($_FILES['userfile']['name']))
          {
          if ($this->upload->do_upload('userfile'))
          {
            // set a $_POST value for 'image' that we can use later
...

顺便说一句,这段代码在我的模型中。

【问题讨论】:

  • 为什么要将它保留在模型中......?
  • 我只是在测试该功能..我知道它应该在控制器中。我稍后会迁移它
  • 点击此链接ellislab.com/codeigniter/user-guide/libraries/… 当您超过maxsize字段中指定的大小时,CI将自动给出错误............

标签: codeigniter codeigniter-2


【解决方案1】:

@Chetan Wadhwa,

您确定大小以字节为单位(不是 KB)。请参阅 codeigniter 文档:http://www.codeigniter.com/user_guide/libraries/file_uploading.html,必须以 KB 为单位。

【讨论】:

    【解决方案2】:

    此大小不是以 KB 为单位,而是以字节为单位,因此对于 10KB,您应该写入 (10*1024) 字节或“10240”

    【讨论】:

    • CodeIgniter 文档指出 max_size 实际上以 KB 为单位。
    【解决方案3】:

    您不需要处理错误消息 CI 会注意在这种情况下,因此请遵循 codeIgniter 用户指南中指定的upload class tutorial

    【讨论】:

      【解决方案4】:
      <?php
      
      class upload extends CI_Controller {
      
      function __construct()
      {
          parent::__construct();
          $this->load->helper(array('form', 'url'));
      }
      
      function index()
      {
          $this->load->view('upload_form', array('error' => ' ' ));
      }
      
      function do_upload()
      {
          $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);
      
          if ( ! $this->upload->do_upload())
          {
              $error = array('error' => $this->upload->display_errors());
      
              $this->load->view('upload_form', $error);
          }
          else
          {
              $data = array('upload_data' => $this->upload->data());
      
              $this->load->view('upload_success', $data);
          }
      
      }
      }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-11
        • 2014-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-26
        • 2019-04-10
        相关资源
        最近更新 更多