亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

CI框架實(shí)現(xiàn)優(yōu)化文件上傳及多文件上傳的方法

 更新時(shí)間:2017年01月04日 08:47:15   作者:Zhihua_W  
這篇文章主要介紹了CI框架實(shí)現(xiàn)優(yōu)化文件上傳及多文件上傳的方法,結(jié)合實(shí)例形式詳細(xì)分析了CI框架優(yōu)化文件上傳及多文件上傳的實(shí)現(xiàn)思路與具體操作步驟,需要的朋友可以參考下

本文實(shí)例分析了CI框架實(shí)現(xiàn)優(yōu)化文件上傳及多文件上傳的方法。分享給大家供大家參考,具體如下:

最近一直在研究Codeigniter框架,開發(fā)項(xiàng)目寫到文件上傳的時(shí)候發(fā)現(xiàn)大部分程序員使用Codeigniter框架的文件上傳類編寫上傳方法的時(shí)候?qū)懙亩即嬖谶@代碼冗余(或者說(shuō)代碼重復(fù)利用率低、比較消耗資源。)故而我研究出一個(gè)稍微優(yōu)化一點(diǎn)的上傳方法。并且在查找資料時(shí)發(fā)現(xiàn),Codeigniter框架同時(shí)上傳多個(gè)文件比較困難,所以在優(yōu)化方法的同時(shí)我又研究了一下如何使用Codeigniter框架實(shí)現(xiàn)同時(shí)上傳多個(gè)文件。下面就來(lái)和大家分享一下,感興趣的同學(xué)可以關(guān)注一下,同時(shí)歡迎大家指正錯(cuò)誤。

1、優(yōu)化文件上傳方法

Codeigniter手冊(cè)里面的那種大家常用的方法在這里就不重復(fù)描述了,下面直接說(shuō)如何對(duì)方法進(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)在項(xià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ù)會(huì)自動(dòng)加載
    $this->load->library('upload');
  }
}

注意:我們?cè)诘谝徊絼?chuàng)建的 “ upload.php ” 文件中的上傳配置信息會(huì)在這里會(huì)自動(dòng)進(jìn)行加載。

c) 編寫上傳方法執(zhí)行do_upload()方法進(jìn)行文件上傳

public function insert()
{
  //設(shè)置驗(yàn)證規(guī)則
  $this->form_validation->set_rules('brand_name','名稱','required');
  if($this->form_validation->run() == false){
    //未通過(guò)驗(yàn)證
    $data['message'] = validation_errors();
    $data['wait'] = 3;
    $data['url'] = site_url('admin/brand/add');
    $this->load->view('message.html',$data);
  }else{
    //通過(guò)驗(yàn)證,處理圖片上傳
    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)用模型完成添加動(dòng)作
      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);
    }
  }
}

注意:上述代碼有部分是我項(xiàng)目中的代碼,大家可以忽略直接關(guān)注關(guān)鍵的上傳代碼。當(dāng)你需要上傳不同的文件時(shí),你也可以在方法中進(jìn)行文件上傳配置,使用$this->upload->initialize()方法進(jìn)行配置。

2、同時(shí)上傳多文件的兩種方法

① 方法一思路:對(duì)所上傳的多個(gè)文件進(jìn)行循環(huán)處理

/**
 * Codeigniter框架實(shí)現(xiàn)多文件上傳
 * @author Zhihua_W
 * 方法一:對(duì)上傳的文件進(jìn)行循環(huán)處理
 */
public function multiple_uploads1()
{
  //載入所需文件上傳類庫(kù)
  $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();
      }
    }
  }
}

② 方法二思路:直接一下將多個(gè)文件全部上傳然后在對(duì)上傳過(guò)的數(shù)據(jù)進(jìn)行處理

/**
 * Codeigniter框架實(shí)現(xiàn)多文件上傳
 * @author Zhihua_W
 * 方法二:直接一下將多個(gè)文件全部上傳然后在對(duì)上傳過(guò)的數(shù)據(jù)進(jìn)行處理
 */
public function multiple_uploads2()
{
  $config['upload_path'] = './public/uploads/';
  //這里的public是相對(duì)于index.php的,也就是入口文件,這個(gè)千萬(wàn)不能弄錯(cuò)!
  //否則就會(huì)報(bào)錯(cuò):"The upload path does not appear to be valid.";
  $config['allowed_types'] = 'gif|jpg|png';
  //我試著去上傳其它類型的文件,這里一定要注意順序!
  //否則報(bào)錯(cuò):"A problem was encountered while attempting to move the uploaded file to the final destination."
  //這個(gè)錯(cuò)誤一般是上傳文件的文件名不能是中文名,這個(gè)很郁悶!還未解決,大家可以用其它方法,重新改一下文件名就可以解決了!
  //$config['allowed_types'] = 'zip|gz|png|gif|jpg';(正確)
  //$config['allowed_types'] = 'png|gif|jpg|zip|gz';(錯(cuò)誤)
  $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>";
    }
  }
}

兩種方法那個(gè)比較方便?那個(gè)比較高效率?大家可以試著自行嘗試一下!

更多關(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è)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論