Home Backend Development PHP Tutorial How to use the CodeIgniter framework to implement image uploading

How to use the CodeIgniter framework to implement image uploading

Jun 14, 2018 pm 02:00 PM
ci codeigniter upload picture

This article mainly introduces the CI (CodeIgniter) framework to implement image uploading methods, and analyzes the relevant operating techniques based on CodeIgniter calling the file upload class to implement the image uploading function in the form of examples. Friends in need can refer to it

The example in this article describes how the CodeIgniter framework implements image uploading. I share it with you for your reference, as follows:

Regarding the commonplace issue of image uploading, I have to repeat it again, because after all, there are some things about this framework that are worth learning and learning from. This article I wrote it with the help of official documents, but some places still need to be marked.

Let’s take a look at picture uploading. First create a view file in the "./application/views/" folder: text.php, the code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

<html>

  <head>

    <title>Upload Form</title>

  </head>

  <body>

      <?php echo $error;?>

      <?php echo form_open_multipart(&#39;upload/do_upload&#39;);?>

      <input type="file" name="userfile" size="20"/>

      <br><br>

      <input type="submit" value="upload"/>

      </form>

  </body>

</html>

Copy after login

Codeigniter has its own very rich upload Class library, let’s take a look at the controller. There is an Upload.php file in the Controller. The code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

class Upload extends CI_Controller{

  public function __construct(){

    parent::__construct();

    $this->load->helper("form","url");

  }

  public function index(){

    $this->load->view(&#39;test&#39;,array("error"=>&#39;&#39;));

  }

  public function do_upload(){

    $config[&#39;upload_path&#39;]=&#39;./uploads/&#39;;

    $config[&#39;allowed_types&#39;]=&#39;gif|jpg|png&#39;;

    $config[&#39;max_size&#39;]=100;

    $config[&#39;max_width&#39;]=1024;

    $config[&#39;max_height&#39;]=768;

    $this->load->library(&#39;upload&#39;,$config);

    if(!$this->upload->do_upload(&#39;userfile&#39;)){

      $error=array(&#39;error&#39;=>$this->upload->display_errors());

      $this->load->view(&#39;test&#39;,$error);

    }else{

      $data=array(&#39;upload_data&#39;=>$this->upload->data());

      $this->load->view(&#39;upload_success&#39;,$data);

    }

  }

}

Copy after login

Next, create another file in the view. upload_success.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<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>

Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. Please pay attention to more related content. PHP Chinese website!

Related recommendations:

About common image processing methods encapsulated in CI framework

About loading views in CI framework views Method

Summary of common functions for CI framework AR database operations

The above is the detailed content of How to use the CodeIgniter framework to implement image uploading. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement custom middleware in CodeIgniter How to implement custom middleware in CodeIgniter Jul 29, 2023 am 10:53 AM

How to implement custom middleware in CodeIgniter Introduction: In modern web development, middleware plays a vital role in applications. They can be used to perform some shared processing logic before or after the request reaches the controller. CodeIgniter, as a popular PHP framework, also supports the use of middleware. This article will introduce how to implement custom middleware in CodeIgniter and provide a simple code example. Middleware overview: Middleware is a kind of request

WeChat applet implements image upload function WeChat applet implements image upload function Nov 21, 2023 am 09:08 AM

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

Steps to implement image uploading and display using CakePHP framework Steps to implement image uploading and display using CakePHP framework Jul 29, 2023 pm 04:21 PM

Steps to implement image upload and display using CakePHP framework Introduction: In modern web applications, image upload and display are common functional requirements. The CakePHP framework provides developers with powerful functions and convenient tools, making it simple and efficient to upload and display images. This article will introduce you to how to use the CakePHP framework to upload and display images. Step 1: Create a file upload form First, we need to create a form in the view file for users to upload images. The following is an example of

CodeIgniter middleware: Accelerate application responsiveness and page rendering CodeIgniter middleware: Accelerate application responsiveness and page rendering Jul 28, 2023 pm 06:51 PM

CodeIgniter Middleware: Accelerating Application Responsiveness and Page Rendering Overview: As web applications continue to grow in complexity and interactivity, developers need to use more efficient and scalable solutions to improve application performance and responsiveness. . CodeIgniter (CI) is a lightweight PHP-based framework that provides many useful features, one of which is middleware. Middleware is a series of tasks that are performed before or after the request reaches the controller. This article will introduce how to use

How to handle image uploading and compression in Vue technology development How to handle image uploading and compression in Vue technology development Oct 08, 2023 am 10:58 AM

How to handle image uploading and compression in Vue technology development In modern web applications, image uploading is a very common requirement. However, due to network transmission and storage reasons, directly uploading original high-resolution images may result in slow upload speeds and a large waste of storage space. Therefore, uploading and compressing images is very important. In Vue technology development, we can use some ready-made solutions to handle image uploading and compression. The following will introduce how to use vue-upload-comone

How to use the database query builder (Query Builder) in the CodeIgniter framework How to use the database query builder (Query Builder) in the CodeIgniter framework Jul 28, 2023 pm 11:13 PM

Introduction to the method of using the database query builder (QueryBuilder) in the CodeIgniter framework: CodeIgniter is a lightweight PHP framework that provides many powerful tools and libraries to facilitate developers in web application development. One of the most impressive features is the database query builder (QueryBuilder), which provides a concise and powerful way to build and execute database query statements. This article will introduce how to use Co

Use PHP framework CodeIgniter to develop a real-time chat application to provide convenient communication services Use PHP framework CodeIgniter to develop a real-time chat application to provide convenient communication services Jun 27, 2023 pm 02:49 PM

With the development of mobile Internet, instant messaging has become more and more important and popular. For many companies, live chat is more like a communication service, providing a convenient communication method that can quickly and effectively solve business problems. Based on this, this article will introduce how to use the PHP framework CodeIgniter to develop a real-time chat application. Understand the CodeIgniter framework CodeIgniter is a lightweight PHP framework that provides a series of simple tools and libraries to help developers quickly

PHP development: Using CodeIgniter to implement MVC pattern and RESTful API PHP development: Using CodeIgniter to implement MVC pattern and RESTful API Jun 16, 2023 am 08:09 AM

As web applications continue to evolve, it is important to develop applications more quickly and efficiently. And, as RESTful API is widely used in web applications, it is necessary for developers to understand how to create and implement RESTful API. In this article, we will discuss how to implement MVC pattern and RESTful API using CodeIgniter framework. Introduction to MVC pattern MVC (Model-Vie

See all articles