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



我想要一些帮助。我找到这个维基是为了用CodeIgniter设置BlueimpJquery上传插件。每件事都很好,但我想做的是将图像上传到合适的相册目录中。

这是我在控制器中的代码

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,

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

$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。

最新更新