CodeIgniter,上传和配置

时间:2012-02-21 03:35:13

标签: codeigniter codeigniter-2

我想使用/config/upload.php配置我的上传。但是,我的一些配置项会根据情况而有所不同。在大多数情况下,上传目录是动态设置的(例如,它包含用户的id,使用随机文件夹等)。有时,可以上传的文件类型会有所不同(例如,只有一种情况下的照片,只有另一种情况下的视频)。

我可以将一般配置项放在/config/upload.php中,稍后添加/覆盖某些内容吗?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:7)

在您用于上传的控制器中,您只需重新定义上传库的选项并重新初始化它们。

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

您将丢失所有预定义的配置项我非常确定,因此您需要重做那些

<强>更新

在进一步研究之后,这可能是可能的。如果你看一下Upload库,你可以在里面找到一些函数set。所有人都没有set个函数,它们可能都不是您可以使用的set函数。所以你可以这样做。

$this->load->library("Upload"); // loads upload library with predefined config items in config/upload.php

//to change upload path
$this->upload->set_upload_path("new location");
//CANNOT DO THIS BECAUSE ITS USED IN do_upload function you would need to extend the upload library and create your own set function.
$this->upload->set_filename("new filename");

$this->upload->do_upload();

其他外观可用于设定值

set_max_filesize
set_max_filename
set_max_width
set_max_height
set_allowed_types
相关问题