CI(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" 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>

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/164c2272ffddf51df0b4d20f123002b5.html