CI框架實現(xiàn)優(yōu)化文件上傳及多文件上傳的方法
本文實例分析了CI框架實現(xiàn)優(yōu)化文件上傳及多文件上傳的方法。分享給大家供大家參考,具體如下:
最近一直在研究Codeigniter框架,開發(fā)項目寫到文件上傳的時候發(fā)現(xiàn)大部分程序員使用Codeigniter框架的文件上傳類編寫上傳方法的時候?qū)懙亩即嬖谶@代碼冗余(或者說代碼重復(fù)利用率低、比較消耗資源。)故而我研究出一個稍微優(yōu)化一點的上傳方法。并且在查找資料時發(fā)現(xiàn),Codeigniter框架同時上傳多個文件比較困難,所以在優(yōu)化方法的同時我又研究了一下如何使用Codeigniter框架實現(xiàn)同時上傳多個文件。下面就來和大家分享一下,感興趣的同學(xué)可以關(guān)注一下,同時歡迎大家指正錯誤。
1、優(yōu)化文件上傳方法
Codeigniter手冊里面的那種大家常用的方法在這里就不重復(fù)描述了,下面直接說如何對方法進(jìn)行優(yōu)化以達(dá)到降低代碼冗余,提高代碼重復(fù)利用率的目的。
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 ” 文件中的上傳配置信息會在這里會自動進(jìn)行加載。
c) 編寫上傳方法執(zhí)行do_upload()方法進(jìn)行文件上傳
public function insert()
{
//設(shè)置驗證規(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)鍵的上傳代碼。當(dāng)你需要上傳不同的文件時,你也可以在方法中進(jìn)行文件上傳配置,使用$this->upload->initialize()方法進(jìn)行配置。
2、同時上傳多文件的兩種方法
① 方法一思路:對所上傳的多個文件進(jìn)行循環(huán)處理
/**
* Codeigniter框架實現(xiàn)多文件上傳
* @author Zhihua_W
* 方法一:對上傳的文件進(jìn)行循環(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ù)進(jìn)行處理
/**
* Codeigniter框架實現(xiàn)多文件上傳
* @author Zhihua_W
* 方法二:直接一下將多個文件全部上傳然后在對上傳過的數(shù)據(jù)進(jìn)行處理
*/
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)框架進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計有所幫助。
相關(guān)文章
php從數(shù)據(jù)庫讀取數(shù)據(jù),并以json格式返回數(shù)據(jù)的方法
今天小編就為大家分享一篇php從數(shù)據(jù)庫讀取數(shù)據(jù),并以json格式返回數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
淺談laravel-admin form中的數(shù)據(jù),在提交后,保存前,獲取并進(jìn)行編輯
今天小編就為大家分享一篇淺談laravel-admin form中的數(shù)據(jù),在提交后,保存前,獲取并進(jìn)行編輯,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
ThinkPHP框架實現(xiàn)數(shù)據(jù)增刪改
本文實例講述了thinkPHP數(shù)據(jù)庫增刪改查操作方法。分享給大家供大家參考。希望對大家學(xué)習(xí)使用thinkPHP有所幫助2017-05-05

