這篇文章主要介紹了CI(CodeIgniter)架構實現圖片上傳的方法,結合執行個體形式分析了基於CodeIgniter調用檔案上傳類實現圖片上傳功能的相關操作技巧,需要的朋友可以參考下
本文執行個體講述了CodeIgniter架構實現圖片上傳的方法。分享給大家供大家參考,具體如下:
對於圖片上傳這種老生常談的問題,在此我不得不再次重複一次,因為對於這架構畢竟有些地方值得自己學習與借鑒,這篇文章我是藉助官方文檔來寫的,但有些地方任然需要標明一下。
下面我們來看看圖片上傳吧。首先在“./application/views/”檔案夾下創一個視圖檔案:text.php,代碼如下:
<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有自己非常豐富upload類庫,下面我們來看看控制器,在Controller中一個Upload.php檔案,代碼如下:
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); } }}
下面在視圖中建立另外一個檔案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>