Home  >  Article  >  Backend Development  >  Image upload in ci framework

Image upload in ci framework

高洛峰
高洛峰Original
2017-02-09 09:52:221391browse

Front-end code

<html>
    <form action="ci/CodeIgniter_2.2.0/index.php/upload/up" method="post" enctype="multipart/form-data">
        <input type="file" name="upfile" />
        <input type="submit" name="sub" value="提交" />
    </form>
</html>

Controller:

Define an array and set some upload-related parameters

$config['upload_path'] = './uploads/';
//设置允许上传的类型
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
//如果是图片还可以设置最大高度和宽度
$config['max_height'] = 768;
$config['max_width'] = 1024;

Call CI’s upload general class and perform upload

//upload为调用的类名,全小写
$this->load->library('upload',$config);
//如果上传框的name写的是userfile,那就不用传参数了,如果不是,把name的值传进去
$this->upload->do_upload('上传框的name');

Receive error message or success message

//出错信息
$error = array('error' => $this->upload->display_error());
//成功信息
$data = array('upload_data' => $this->upload->data());

<?php 
if ( ! defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);

class Upload extends CI_Controller {
    //显示带表单的视图
    public function index(){
        $this->load->view('up');
    }
    //显示上传信息
    public function up(){
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = "2000";
        $this->load->library('upload',$config);
        //打印成功或错误的信息
        if($this->upload->do_upload('upfile'))
        {
            $data = array("upload_data" => $this->upload->data());
            var_dump($data);
        }
        else
        {
            $error = array("error" => $this->upload->display_errors());
            var_dump($error);
        }
    }
}

                                                 


Front-end code

<html>
    <form action="ci/CodeIgniter_2.2.0/index.php/upload/up" method="post" enctype="multipart/form-data">
        <input type="file" name="upfile" />
        <input type="submit" name="sub" value="提交" />
    </form>
</html>

Controller:

Define an array and set some upload-related parameters

$config['upload_path'] = './uploads/';
//设置允许上传的类型
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
//如果是图片还可以设置最大高度和宽度
$config['max_height'] = 768;
$config['max_width'] = 1024;

Call CI's upload general class and execute the upload

//upload为调用的类名,全小写
$this->load->library('upload',$config);
//如果上传框的name写的是userfile,那就不用传参数了,如果不是,把name的值传进去
$this->upload->do_upload('上传框的name');

Receive error or success information

//出错信息
$error = array('error' => $this->upload->display_error());
//成功信息
$data = array('upload_data' => $this->upload->data());

<?php 
if ( ! defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);

class Upload extends CI_Controller {
    //显示带表单的视图
    public function index(){
        $this->load->view('up');
    }
    //显示上传信息
    public function up(){
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = "2000";
        $this->load->library('upload',$config);
        //打印成功或错误的信息
        if($this->upload->do_upload('upfile'))
        {
            $data = array("upload_data" => $this->upload->data());
            var_dump($data);
        }
        else
        {
            $error = array("error" => $this->upload->display_errors());
            var_dump($error);
        }
    }
}

For more image uploading in the ci framework, please pay attention to the PHP Chinese website for related articles !           


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn