# CodeIgniter 文件上传类 - 控制器

**URL:** <https://www.isharkfly.com/t/codeigniter/5149>\
**Category:** PHP\
**Created:** [January 8, 2014, 2:27am UTC](https://www.isharkfly.com/t/codeigniter/5149 "2014-01-08T02:27:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [January 8, 2014, 2:27am UTC](https://www.isharkfly.com/t/codeigniter/5149/1 "2014-01-08T02:27:53Z")

</div>

运用文本编辑器，创建一个名为upload.php的控制器。

复制以下代码并保存到applications/controllers/ 目录里：[code]\<?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);  
}  
}  
}  
?\>[/code]文章来源：[http://cwiki.ossez.com/pages/viewpage.action?pageId=2392149](http://cwiki.ossez.com/pages/viewpage.action?pageId=2392149)
