1、创建表单 upload_form.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<form action="<?php echo site_url('upload/do_upload') ?>" method="post" class="form-horizontal" enctype="multipart/form-data">
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
2、写上传程序upload.php
<?php
/*constructor function to initialize controller and load the file upload class,
plus the two other helpers it needs */
class Upload extends CI_Controller {
function Upload()
{
//parent::Controller();
parent::__construct(
);
}
/*now the function which does all the work!*/
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', $error);
}else{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
// $this->upload->do_upload();
// $data = array('upload_data' => $this->upload->data());
// $this->load->view('upload_success', $data);
}
}
3、调用 在welcome入口控制器 调用视图upload_form
public function index()
{
// $this->load->view('header');
// $this->load->view('index');
// $this->load->view('footer');
$this->load->view('upload_form');
}
4、上传成功的页面,并输出上传后的文件路径 upload_success.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach($upload_data as $item => $value):?>
<?php if($item=='full_path'){
echo $value;
}
?>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
5、上传失败的显示页面 upload_form_error.php