[eluser]Jazzerus[/eluser]
I noticed one more problem with the code I had. For some reason, the first time the library was loaded, it cached the $config['upload_path'], so the thumbnail was not going into the right directory, even though I was passing a new value for $config['upload_path']. This appeared to be the only $config[] value that got cached. Additionally the filename was getting cached.
The main image was supposed to go into /images/uploads/products/ and it was.
The thumbnail was supposed to go into /images/uploads/products/thumbs/ but instead it was going into the main image directory instead and even though it was using the thumbnail file, it was using the same filename as the main image (but simply appending the file extension (myimage.gif would become myimage.gif.gif)).
The only solution I could think of to solve this would be to somehow unload the library and load it up again with the new settings, but there is no $this->unload->library('upload') or $this->unload('upload'). (Maybe in a future version?)
Fortunately, there is a solution. The CI Loader class (the object that is called whenever you do a $this->load-> command) has a third argument that can be passed to it when loading a library:
Code:
$this->load->library('class_name', $config, 'object name');
The third argument allows you to set a name for the object that is instantiated. By creating a new object the second time the _handle_image_upload function is called, the new $config['upload_path'] is used.
My _handle_image_upload function now looks like this:
Code:
function _handle_image_upload($config, $field_name, $status) {
// set base configuration options for library
$base_config = array();
$base_config['allowed_types'] = "jpg|png|gif";
$base_config['max_size'] = 3072;
$config = array_merge($base_config, $config);
if (!isset($this->upload)) {
$this->load->library('upload', $config);
$obj_name = "upload";
} else {
$this->load->library('upload', $config, "upload_2");
$obj_name = "upload_2";
}
if ($this->$obj_name->do_upload($field_name)) {
$status = $this->$obj_name->data();
return true;
} else {
$status = $this->$obj_name->display_errors();
return false;
}
}