CodeIgniter框架图片上传

来源:互联网 发布:future说唱知乎 编辑:程序博客网 时间:2024/06/07 06:20

对于图片上传这种老生常谈的问题,在此我不得不再次重复一次,因为这是一个新的框架,毕竟有些地方值得自己学习与借鉴,这篇博客我是借助官方文档来写的,但有些地方任然需要标明一下。

下面我们来看看图片上传吧。首先在“./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>
0 0
原创粉丝点击