本文实例讲述了CodeIgniter框架实现图片上传的方法。分享给大家供大家参考,具体如下:对
本文实例讲述了CodeIgniter框架实现图片上传的方法。分享给大家供大家参考,具体如下:
对于图片上传这种老生常谈的问题,在此我不得不再次重复一次,因为对于这框架毕竟有些地方值得自己学习与借鉴,这篇文章我是借助官方文档来写的,但有些地方任然需要标明一下。
下面我们来看看图片上传吧。首先在“./application/views/”文件夹下创一个视图文件:text.php,代码如下:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20"/>
<br><br>
<input type="submit" value="upload"/>
</form>
</body>
</html>
Codeigniter有自己非常丰富upload类库,下面我们来看看控制器,在Controller中一个Upload.php文件,代码如下:
class Upload extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->helper("form","url");
}
public function index(){
$this->load->view('test',array("error"=>''));
}
public 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('userfile')){
$error=array('error'=>$this->upload->display_errors());
$this->load->view('test',$error);
}else{
$data=array('upload_data'=>$this->upload->data());
$this->load->view('upload_success',$data);
}
}
}
下面在视图中创建另外一个文件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):?>
<li>
<?php echo $item;?>:<?php echo $value;?>
</li>
<?php?>
</ul>
</body>
</html>
CI
CodeIgniter
图片上传