How to use the CodeIgniter framework to implement image uploading

不言
Release: 2023-04-01 07:34:01
Original
1411 people have browsed it

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:

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

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

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!