CodeIgniter, upload and config
-
28-10-2019 - |
Question
I'd like to config my upload using /config/upload.php. However, some of my config items will vary depending on the situation. In most cases, the upload directory is dynamically set (e.g. it incorporates the user's id, uses a random folder, etc.). Sometimes, the type of files that can be uploaded will be different (e.g. only photos in one case, only videos in another case).
Can I put the general config items in /config/upload.php and add/override certain things later? If so, how?
Solution
In your controller that you are using to upload you just need to redefine the options for the upload library and initialize them again.
$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);
You will lose all your predefined config items I'm pretty sure so you'll need to redo those
UPDATE
After looking into this a little more this may be possible. If you look at the Upload
library there are functions inside where you can set
some of the variables. There aren't set
functions for all and they may not all be a set
function that you can use. So you could do something like this.
$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();
Others that look like can be used to set values
set_max_filesize
set_max_filename
set_max_width
set_max_height
set_allowed_types