2013-04-25 158 views
1

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

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 
... 

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

+0

为什么你要保持它在你的模型....? – 2013-04-25 06:46:11

+0

我只是测试功能..我知道它应该在控制器中。我将稍后迁移 – 2013-04-25 06:47:02

+0

当您超出maxsize字段中指定的大小时,请遵循此链接http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html CI将自动给出错误........ – 2013-04-25 06:47:58

回答

0

你不需要处理错误信息在这种情况下,CI会小心谨慎,所以请遵循codeIgniter用户指南中指定的upload class tutorial

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

这个尺寸不KB它的字节因此对于10KB你应该写(10 * 1024)字节或 “10240”

+0

CodeIgniter文档指出max_size实际上是KB。 – itsols 2015-06-21 10:53:29