CodeIgniter,上传和配置

CodeIgniter,上传和配置,codeigniter,codeigniter-2,Codeigniter,Codeigniter 2,我想使用/config/upload.php配置我的上传。但是,我的一些配置项将根据情况而有所不同。在大多数情况下,上载目录是动态设置的,例如,它包含用户id、使用随机文件夹等。。有时,可以上载的文件类型会有所不同,例如,在一种情况下,只能上载照片,在另一种情况下,只能上载视频 我可以将常规配置项放在/config/upload.php中,然后添加/覆盖某些内容吗?如果是,如何进行?在用于上载的控制器中,只需重新定义上载库的选项并再次初始化它们 $config['upload_path'] =

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


我可以将常规配置项放在/config/upload.php中,然后添加/覆盖某些内容吗?如果是,如何进行?

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

$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);
您将丢失所有预定义的配置项,我非常确定,因此您需要重做这些配置项

更新

在进一步研究之后,这可能是可能的。如果查看上载库,可以在其中设置一些变量。没有针对所有对象的集合函数,它们也可能不是您可以使用的集合函数。所以你可以这样做

$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

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

$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);
您将丢失所有预定义的配置项,我非常确定,因此您需要重做这些配置项

更新

在进一步研究之后,这可能是可能的。如果查看上载库,可以在其中设置一些变量。没有针对所有对象的集合函数,它们也可能不是您可以使用的集合函数。所以你可以这样做

$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

那不是我想做的。我只想覆盖某些配置值。如果每次上传时我都要这样做,那么使用/config/upload.php是没有意义的。@Henesnarfel,效果很好。谢谢:我使用abmove方法重新定义了允许的文件类型。@Henesnarfel:如果我使用:$this->upload->set\u upload\u pathnew location;我还需要再次初始化库吗?@Lykos不,你不应该这样做。您将在初始化后进行该调用以更改默认值。所有这些函数所做的就是在初始化后更改值。因此,如果要上载多个文件,可以使用foreach并为每个文件调用set_upload_path来更改每个文件的上载路径,而无需每次初始化。我没有这样做,但我认为应该这样做。这不是我想做的。我只想覆盖某些配置值。如果每次上传时我都要这样做,那么使用/config/upload.php是没有意义的。@Henesnarfel,效果很好。谢谢:我使用abmove方法重新定义了允许的文件类型。@Henesnarfel:如果我使用:$this->upload->set\u upload\u pathnew location;我还需要再次初始化库吗?@Lykos不,你不应该这样做。您将在初始化后进行该调用以更改默认值。所有这些函数所做的就是在初始化后更改值。因此,如果要上载多个文件,可以使用foreach并为每个文件调用set_upload_path来更改每个文件的上载路径,而无需每次初始化。我没有这样做,但我认为应该这样做。