本文實例分析了CI框架實現優化文件上傳及多文件上傳的方法。分享給大家供大家參考,具體如下:
最近一直在研究Codeigniter框架,開發項目寫到文件上傳的時候發現大部分程序員使用Codeigniter框架的文件上傳類編寫上傳方法的時候寫的都存在這代碼冗余(或者說代碼重復利用率低、比較消耗資源。)故而我研究出一個稍微優化一點的上傳方法。并且在查找資料時發現,Codeigniter框架同時上傳多個文件比較困難,所以在優化方法的同時我又研究了一下如何使用Codeigniter框架實現同時上傳多個文件。下面就來和大家分享一下,感興趣的同學可以關注一下,同時歡迎大家指正錯誤。
1、優化文件上傳方法
Codeigniter手冊里面的那種大家常用的方法在這里就不重復描述了,下面直接說如何對方法進行優化以達到降低代碼冗余,提高代碼重復利用率的目的。
a) 首先在 “ application/config ” 新建 " upload.php " 配置文件
在 “ application/config ” 新建 " upload.php" 配置文件,在里面寫入上傳的配置參數。
1
2
3
4
5
6
7
8
|
<?php defined( 'BASEPATH' ) OR exit ( 'No direct script access allowed' ); //上傳的參數配置 $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參數所代表的路徑文件夾你已經在項目中創建完畢!
b) 在控制器的構造函數中加載文件上傳類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?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' ); //激活分析器以調試程序 $this ->output->enable_profiler(TRUE); //配置中上傳的相關參數會自動加載 $this ->load->library( 'upload' ); } } |
注意:我們在第一步創建的 “ upload.php ” 文件中的上傳配置信息會在這里會自動進行加載。
c) 編寫上傳方法執行do_upload()方法進行文件上傳
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
public function insert() { //設置驗證規則 $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' ]; //獲取表單提交數據 $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' ); //調用模型完成添加動作 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 ); } } } |
注意:上述代碼有部分是我項目中的代碼,大家可以忽略直接關注關鍵的上傳代碼。當你需要上傳不同的文件時,你也可以在方法中進行文件上傳配置,使用$this->upload->initialize()方法進行配置。
2、同時上傳多文件的兩種方法
① 方法一思路:對所上傳的多個文件進行循環處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/** * Codeigniter框架實現多文件上傳 * @author Zhihua_W * 方法一:對上傳的文件進行循環處理 */ public function multiple_uploads1() { //載入所需文件上傳類庫 $this ->load->library( 'upload' ); //配置上傳參數 $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 ); //循環處理上傳文件 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(); } } } } |
② 方法二思路:直接一下將多個文件全部上傳然后在對上傳過的數據進行處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/** * Codeigniter框架實現多文件上傳 * @author Zhihua_W * 方法二:直接一下將多個文件全部上傳然后在對上傳過的數據進行處理 */ 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>" ; } } } |
兩種方法那個比較方便?那個比較高效率?大家可以試著自行嘗試一下!
希望本文所述對大家基于CodeIgniter框架的PHP程序設計有所幫助。