CodeIgniter中的上传大小限制

3

我有一个页面,用户可以在其中注册。在此过程中,他会上传个人资料照片。我想限制照片的大小,但是在codeigniter文档中并没有太多关于$config ['maxsize']的强调。我尝试了以下方法,但是没有收到任何消息。我将大小设置为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
...

顺便提一下,这段代码在我的模型里。


为什么你要把它放在你的模型里面呢? - Venkata Krishna
我只是在测试这个功能,我知道它应该在控制器中。稍后我会迁移它。 - sys_debug
当你超过maxsize字段中指定的大小时,CI会自动给出错误。请点击此链接http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html。 - Venkata Krishna
4个回答

4

1

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


1
CodeIgniter文档指出,max_size实际上是以KB为单位的。 - itsols

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

}
}
?>

0

您不需要处理错误消息,CI会在这种情况下处理它,请按照CodeIgniter用户指南中指定的上传类教程进行操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接