codeigniter上传配置

时间:2010-10-08 08:03:07

标签: php codeigniter

如何在config / upload.php中指定多个配置?

2 个答案:

答案 0 :(得分:4)

我认为这样做是不可能的,手册说:

  

在配置文件中设置首选项

     

如果您不想设置偏好设置   使用上面的方法,你可以   而是将它们放入配置文件中。   只需创建一个名为的新文件   upload.php,添加$ config数组   那个文件。然后将文件保存在:   config / upload.php将被使用   自动。你不需要   使用$ this-> upload->初始化   如果你保存你的喜好,功能   在配置文件中。

所以你添加到$ config array()而没有任何自动初始化的键。建立一个配置文件并使用你的配置参数加载它可能会更好:

$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);

希望它有所帮助!

答案 1 :(得分: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'));