CI框架實現(xiàn)優(yōu)化文件上傳及多文件上傳的方法
本文實例分析了CI框架實現(xiàn)優(yōu)化文件上傳及多文件上傳的方法。分享給大家供大家參考,具體如下:
最近一直在研究Codeigniter框架,開發(fā)項目寫到文件上傳的時候發(fā)現(xiàn)大部分程序員使用Codeigniter框架的文件上傳類編寫上傳方法的時候?qū)懙亩即嬖谶@代碼冗余(或者說代碼重復利用率低、比較消耗資源。)故而我研究出一個稍微優(yōu)化一點的上傳方法。并且在查找資料時發(fā)現(xiàn),Codeigniter框架同時上傳多個文件比較困難,所以在優(yōu)化方法的同時我又研究了一下如何使用Codeigniter框架實現(xiàn)同時上傳多個文件。下面就來和大家分享一下,感興趣的同學可以關(guān)注一下,同時歡迎大家指正錯誤。
1、優(yōu)化文件上傳方法
Codeigniter手冊里面的那種大家常用的方法在這里就不重復描述了,下面直接說如何對方法進行優(yōu)化以達到降低代碼冗余,提高代碼重復利用率的目的。
a) 首先在 “ application/config ” 新建 " upload.php " 配置文件
在 “ application/config ” 新建 " upload.php" 配置文件,在里面寫入上傳的配置參數(shù)。
<?php defined('BASEPATH') OR exit('No direct script access allowed'); //上傳的參數(shù)配置 $config['upload_path'] = './public/uploads/'; $config['allowed_types'] = 'gif|png|jpg'; $config['max_size'] = 100; $config['max_width'] = '1024'; $config['max_height'] = '768';
注意:upload_path參數(shù)所代表的路徑文件夾你已經(jīng)在項目中創(chuàng)建完畢!
b) 在控制器的構(gòu)造函數(shù)中加載文件上傳類
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * 控制器 */ class Brand extends Admin_Controller { public function __construct() { parent::__construct(); $this->load->model('brand_model'); $this->load->library('form_validation'); //激活分析器以調(diào)試程序 $this->output->enable_profiler(TRUE); //配置中上傳的相關(guān)參數(shù)會自動加載 $this->load->library('upload'); } }
注意:我們在第一步創(chuàng)建的 “ upload.php ” 文件中的上傳配置信息會在這里會自動進行加載。
c) 編寫上傳方法執(zhí)行do_upload()方法進行文件上傳
public function insert() { //設置驗證規(guī)則 $this->form_validation->set_rules('brand_name','名稱','required'); if($this->form_validation->run() == false){ //未通過驗證 $data['message'] = validation_errors(); $data['wait'] = 3; $data['url'] = site_url('admin/brand/add'); $this->load->view('message.html',$data); }else{ //通過驗證,處理圖片上傳 if ($this->upload->do_upload('logo')) { //logo為前端file控件名 //上傳成功,獲取文件名 $fileInfo = $this->upload->data(); $data['logo'] = $fileInfo['file_name']; //獲取表單提交數(shù)據(jù) $data['brand_name'] = $this->input->post('brand_name'); $data['url'] = $this->input->post('url'); $data['brand_desc'] = $this->input->post('brand_desc'); $data['sort_order'] = $this->input->post('sort_order'); $data['is_show'] = $this->input->post('is_show'); //調(diào)用模型完成添加動作 if($this->brand_model->add_brand($data)){ $data['message'] = "添加成功"; $data['wait'] = 3; $data['url'] = site_url('admin/brand/index'); $this->load->view('message.html',$data); }else{ $data['message'] = "添加失敗"; $data['wait'] = 3; $data['url'] = site_url('admin/brand/add'); $this->load->view('message.html',$data); } }else{ //上傳失敗 $data['message'] = $this->upload->display_errors(); $data['wait'] = 3; $data['url'] = site_url('admin/brand/add'); $this->load->view('message.html',$data); } } }
注意:上述代碼有部分是我項目中的代碼,大家可以忽略直接關(guān)注關(guān)鍵的上傳代碼。當你需要上傳不同的文件時,你也可以在方法中進行文件上傳配置,使用$this->upload->initialize()方法進行配置。
2、同時上傳多文件的兩種方法
① 方法一思路:對所上傳的多個文件進行循環(huán)處理
/** * Codeigniter框架實現(xiàn)多文件上傳 * @author Zhihua_W * 方法一:對上傳的文件進行循環(huán)處理 */ public function multiple_uploads1() { //載入所需文件上傳類庫 $this->load->library('upload'); //配置上傳參數(shù) $upload_config = array( 'upload_path' => './public/uploads/', 'allowed_types' => 'jpg|png|gif', 'max_size' => '500', 'max_width' => '1024', 'max_height' => '768', ); $this->upload->initialize($upload_config); //循環(huán)處理上傳文件 foreach ($_FILES as $key => $value) { if (!empty($key['name'])) { if ($this->upload->do_upload($key)) { //上傳成功 print_r($this->upload->data()); } else { //上傳失敗 echo $this->upload->display_errors(); } } } }
② 方法二思路:直接一下將多個文件全部上傳然后在對上傳過的數(shù)據(jù)進行處理
/** * Codeigniter框架實現(xiàn)多文件上傳 * @author Zhihua_W * 方法二:直接一下將多個文件全部上傳然后在對上傳過的數(shù)據(jù)進行處理 */ public function multiple_uploads2() { $config['upload_path'] = './public/uploads/'; //這里的public是相對于index.php的,也就是入口文件,這個千萬不能弄錯! //否則就會報錯:"The upload path does not appear to be valid."; $config['allowed_types'] = 'gif|jpg|png'; //我試著去上傳其它類型的文件,這里一定要注意順序! //否則報錯:"A problem was encountered while attempting to move the uploaded file to the final destination." //這個錯誤一般是上傳文件的文件名不能是中文名,這個很郁悶!還未解決,大家可以用其它方法,重新改一下文件名就可以解決了! //$config['allowed_types'] = 'zip|gz|png|gif|jpg';(正確) //$config['allowed_types'] = 'png|gif|jpg|zip|gz';(錯誤) $config['max_size'] = '1024'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $config['file_name'] = time(); //文件名不使用原始名 $this->load->library('upload', $config); if (!$this->upload->do_upload()) { echo $this->upload->display_errors(); } else { $data['upload_data'] = $this->upload->data(); //上傳文件的一些信息 $img = $data['upload_data']['file_name']; //取得文件名 echo $img . "<br>"; foreach ($data['upload_data'] as $item => $value) { echo $item . ":" . $value . "<br>"; } } }
兩種方法那個比較方便?那個比較高效率?大家可以試著自行嘗試一下!
更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于CodeIgniter框架的PHP程序設計有所幫助。
相關(guān)文章
實例分析PHP將字符串轉(zhuǎn)換成數(shù)字的方法
在本篇文章里我們給大家分享的是關(guān)于PHP將字符串轉(zhuǎn)換成數(shù)字的方法和相關(guān)知識點,有興趣的朋友們學習下。2019-01-01Thinkphp實現(xiàn)站點靜態(tài)化的方法詳解
本篇文章主要介紹了Thinkphp實現(xiàn)站點靜態(tài)化的方法,thinkphp提供了一個有效的生成靜態(tài)頁的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03利用Fix Rss Feeds插件修復WordPress的Feed顯示錯誤
這篇文章主要介紹了利用Fix Rss Feeds插件修復WordPress的Feed顯示錯誤的方法,需要的朋友可以參考下2015-12-12PHP設計模式之中介者模式(Mediator Pattern)入門與應用案例詳解
這篇文章主要介紹了PHP設計模式之中介者模式(Mediator Pattern),結(jié)合實例形式詳細分析了PHP中介者模式的基本概念、原理、應用案例與相關(guān)操作注意事項,需要的朋友可以參考下2019-12-12GD庫實現(xiàn)webp轉(zhuǎn)換jpg的PHP程序
PHP程序來執(zhí)行webp格式轉(zhuǎn)換成jpg格式有幾種方法:一是安裝imagemagick實現(xiàn),二是安裝GD庫實現(xiàn),可以直接用dwebp命令,本文我們將介紹使用PHP的圖像處理庫GD,編寫一個簡單的PHP程序來完成這個任務2024-03-03