Search code examples
jquerycodeigniterfile-uploadjquery-file-uploadblueimp

Set dynamic upload dir in jQuery Blueimp Upload Plugin?


I'd like some help please. I have found this wiki in order to setup Blueimp Jquery Upload Plugin with CodeIgniter. Every thing works fine, but what I'd like to do is upload images on the appropriate album-directories.

This is my code in the controller

public function upload(){
// get the album directory in order to upload images to the correct album
$id = $this->input->post('album');
        $album = $this->album_model->get_album_dir($id);

// now I need pass the album directory to the UploadHandler, 
// in order to specify the correct upload path
        $this->load->library('UploadHandler');
    }

and this is the main code for settings inside UploadHandler

function __construct($options = null, $initialize = true, $error_messages = null) {
        $this->options = array(
            'script_url' => $this->get_full_url().'/admin/upload',
            'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/uploads/albums/' . <PUT-ALBUM-DIRECTORY-HERE>,
            'upload_url' => $this->get_full_url().'/uploads/albums/' . . <PUT-ALBUM-DIRECTORY-HERE>,

UPDATE: This is my code in controller

public function upload(){
        // get the album directory in order to upload images to the correct album
    $id = $this->input->post('album');
            $album = $this->album_model->get_album_dir($id);


        $options = array(
            'script_path' => '/admin/upload',
            'upload_dir' => '/uploads/albums/'. $album,
            'upload_url' => '/uploads/albums/'. $album,
            'accept_file_types' => '/\.(gif|jpe?g|png)$/i',
        );
        $this->load->library('UploadHandler', $options);
    }

and this is the code inside UploadHandler.php

function __construct($options = null, $initialize = true, $error_messages = null) {

        $script_url = isset($options['script_path']) ? $options['script_path'] : '/';
        $upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : '/files/';
        $upload_url = isset($options['upload_url']) ? $options['upload_url'] : '/files/';
        $accept_file_types = isset($options['accept_file_types']) ? $options['accept_file_types'] : '/.+$/i' ;

        $this->options = array(
            'script_url' => $this->get_full_url(). $script_url,
            'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')). $upload_dir,
            'upload_url' => $this->get_full_url(). $upload_url,

Solution

  • according to the Documentation you can pass parameters to library while initializing.

    $params = array('type' => 'large', 'color' => 'red');
    
    $this->load->library('Someclass', $params); 
    

    so in your case it should be

        $album = $this->album_model->get_album_dir($id);
        $options= array("upload_dir"=>'dir here',"upload_url"=>'url here');
        $this->load->library('UploadHandler',$options); // you can set the options of Upload handler
    

    now in upload_handler.php

    'upload_dir' => if(isset($options['upload_dir'])) ? $options['upload_dir'] : "default dir for all",
    'upload_url' => if(isset($options['upload_url'])) ? $options['upload_url'] : "default url for all",
    

    Note : you can do further test instead of just isset.