Home > Backend Development > PHP Tutorial > Image upload in ci framework

Image upload in ci framework

高洛峰
Release: 2023-03-05 15:48:01
Original
1401 people have browsed it

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>
Copy after login
Copy after login

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;
Copy after login
Copy after login

Call CI’s upload general class and perform upload

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

Receive error message or success message

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

<?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);
        }
    }
}
Copy after login
Copy after login

                                                 


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>
Copy after login
Copy after login

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;
Copy after login
Copy after login

Call CI's upload general class and execute the upload

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

Receive error or success information

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

<?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);
        }
    }
}
Copy after login
Copy after login

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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template