在jQuery Blueimp上传插件中设置动态上传目录?

时间:2014-10-09 09:30:15

标签: jquery codeigniter file-upload jquery-file-upload blueimp

我想请一些帮助。为了使用CodeIgniter设置Blueimp Jquery Upload Plugin,我找到了这个wiki。每件事情都很好,但我想做的是在适当的专辑目录上上传图片。

这是我在控制器中的代码

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');
    }

这是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>,

更新:这是我在控制器中的代码

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

这是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,

1 个答案:

答案 0 :(得分:0)

根据Documentation,您可以在初始化时将参数传递给库。

$params = array('type' => 'large', 'color' => 'red');

$this->load->library('Someclass', $params); 

所以在你的情况下它应该是

    $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

现在在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",

注意:您可以进行进一步的测试,而不仅仅是isset。