Home  >  Article  >  Backend Development  >  Detailed explanation of how CI framework implements image uploading

Detailed explanation of how CI framework implements image uploading

黄舟
黄舟Original
2017-03-27 09:34:271736browse

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>

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);
    }
  }
}

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