在codeigniter中上传大小限制

时间:2013-04-25 06:44:31

标签: codeigniter codeigniter-2

我有一个用户可以注册的页面。他在此过程中上传了个人资料照片。我想限制大小,但除了$ 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
...

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

4 个答案:

答案 0 :(得分:3)

@Chetan Wadhwa,

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

<?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);
    }

}
}
?>