Home > Article > Backend Development > Detailed explanation of how CI framework implements image uploading
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('upload/do_upload');?> <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('test',array("error"=>'')); } public function do_upload(){ $config['upload_path']='./uploads/'; $config['allowed_types']='gif|jpg|png'; $config['max_size']=100; $config['max_width']=1024; $config['max_height']=768; $this->load->library('upload',$config); if(!$this->upload->do_upload('userfile')){ $error=array('error'=>$this->upload->display_errors()); $this->load->view('test',$error); }else{ $data=array('upload_data'=>$this->upload->data()); $this->load->view('upload_success',$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!