Home Backend Development PHP Tutorial Detailed explanation of how CI framework implements image uploading

Detailed explanation of how CI framework implements image uploading

Mar 27, 2017 am 09:34 AM
ci codeigniter upload picture

This article mainly introduces the CI (CodeIgniter) framework to implement the method of pictureupload, and analyzes the related operations of calling the file upload class based on CodeIgniter to implement the image upload function in the form of examples. For tips, friends who need them can refer to

. The example in this article describes how to upload images using the CodeIgniter framework. 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:

<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 control Controller, there is an Upload.php file in the Controller, the code is as follows:

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 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>
Copy after login
in the view

The above is the detailed content of Detailed explanation of how CI framework implements 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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

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

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

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

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

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

CI vs Laravel: Which framework is better for building a blog or CMS website? CI vs Laravel: Which framework is better for building a blog or CMS website? Jun 19, 2023 am 08:54 AM

With the continuous development and upgrading of website construction, choosing a framework that suits you has become an essential skill for website builders. This article will briefly analyze CI and Laravel to help you choose a framework that is more suitable for building a blog or CMS website. 1. Introduction to CI CodeIgniter, referred to as CI, is an open source lightweight web application development framework that adopts the MVC architecture model. CI can run on PHP5.2 and above, and includes many commonly used libraries and helper functions, making it possible to use CI to develop web applications.

See all articles