[英]CodeIgniter File Upload Class - Initialize different config file
所以我已經在谷歌搜索了一下,似乎找不到答案。
據我了解,這段代碼: $this->upload->initialize()
使用upload.php
配置文件初始化CI文件上載類。 我想做的是使用其他文件。
我嘗試了$this->upload->initialize('upload_other')
,但這似乎不起作用。 我知道您可以在控制器中設置一個$config
數組,但是我試圖避免這種情況。
這可能嗎? 我是以錯誤的方式接近這個嗎?
您不能像這樣初始化/覆蓋配置。
您可以通過以下方式初始化
$this->config->load('upload');
-- Some code Here --
$this->config->load('upload_other');
-- Some code Here --
或者,您可以按以下方式按數組進行操作。
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
$this->upload->initialize($config);
如果要同時上傳其他文件,則可以更改配置數組。
$config2['upload_path'] = './uploads/small/';
$config2['allowed_types'] = 'gif|jpg|png';
$config2['max_size'] = '100';
$config2['max_width'] = '100';
$config2['max_height'] = '100';
$this->load->library('upload', $config2);
// Alternately you can set
$this->upload->initialize($config2);
UPDATE
您可以在配置文件中指定常規數據。 說
config['width'] = '100';
config['width2'] = '100';
現在在您的控制器中使用
config['width'] = $this->config->item('width');
config2['width'] = $this->config->item('width2');
這樣,您可以重復使用相同的設置。
您為什么要嘗試避免使用配置數組? 另一種方法是創建upload.php-config文件。 如果要跨不同的控制器使用不同的配置,則始終可以創建並加載完整的自定義配置文件: Codeigniter用戶指南在這里,您可以使用不同的upload-config數組創建多個變量。
您可以在每個控制器中加載此配置文件,並通過使用initialize方法使用這些配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.