2010-10-08 46 views

回答

3

我認爲這是沒有更多鈔票去做,手冊上說:

設置首選項在配置文件中

如果你不喜歡使用上述方法來設置首選項 ,你可以用 代替它們到一個配置文件中。 只需創建一個名爲 upload.php的新文件,在 文件中添加$ config數組。然後將該文件保存在: config/upload.php中,並自動使用 。如果您將首選項 保存在配置文件中,則不需要 使用$ this-> upload->初始化 函數。

因此,您要添加到$ config array(),而沒有任何鍵來自動初始化。可能會更好做一個配置文件,並與你的配置PARAMS加載,如:與

$config['upload_1']['upload_path'] = './uploads/'; 
$config['upload_1']['allowed_types'] = 'gif|jpg|png'; 
$config['upload_1']['max_size'] = '100'; 
$config['upload_1']['max_width'] = '1024'; 
$config['upload_1']['max_height'] = '768'; 
後來在你的控制器

和加載:

$this->load->config('upload_vals', TRUE); 

$upload_vals = $this->config->item('upload_1'); 

$this->load->library('upload', $upload_vals); 

希望它可以幫助!

0

這是另一種方法。

application/config/upload.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

$config = array(
    'member_photo' => array(
     'upload_path' => './uploads/member_photos/', 
     'allowed_types' => 'gif|jpeg|jpg|png', 
     'max_size'  => '0', 
     'max_width'  => '0', 
     'max_height' => '0', 
     'encrypt_name' => true 
    ), 
    'pet_photo' => array(
     'upload_path' => './uploads/pet_photos/', 
     'allowed_types' => 'gif|jpeg|jpg|png', 
     'max_size'  => '0', 
     'max_width'  => '0', 
     'max_height' => '0', 
     'encrypt_name' => true 
    ), 
); 

application/libraries/MY_Upload.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class MY_Upload extends CI_Upload 
{ 

    function initialize($config = array()) 
    { 
     // load config 
     if(!empty($config['config'])) 
     { 
      $CI =& get_instance(); 
      $CI->load->config('upload'); 
      $autoload_config = $CI->config->item($config['config']); 

      if($autoload_config) 
      { 
       foreach($autoload_config as $key => $val) 
       { 
        if(empty($config[$key])) 
        { 
         $config[$key] = $val; 
        } 
       } 
      } 

      unset($config['config']); 
     } 

     parent::initialize($config); 
    } 

} 
在控制器

即可;

$this->load->library('upload', array('config' => 'member_photo'));

:您定義將覆蓋在配置文件中的任何附加鍵