溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CI框架如何實現(xiàn)優(yōu)化文件上傳

發(fā)布時間:2021-06-30 15:55:47 來源:億速云 閱讀:194 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了CI框架如何實現(xiàn)優(yōu)化文件上傳,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

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

Codeigniter手冊里面的那種大家常用的方法在這里就不重復(fù)描述了,下面直接說如何對方法進行優(yōu)化以達到降低代碼冗余,提高代碼重復(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 ” 文件中的上傳配置信息會在這里會自動進行加載。

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

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)鍵的上傳代碼。當你需要上傳不同的文件時,你也可以在方法中進行文件上傳配置,使用$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>";
    }
  }
}

感謝你能夠認真閱讀完這篇文章,希望小編分享的“CI框架如何實現(xiàn)優(yōu)化文件上傳”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI