Custom $options directory for jQuery Blueimp Upload Plugin?

Deadly 提交于 2021-02-07 19:42:50

问题


I have been working with the blueimp jQuery File Upload plugin and I'm trying to setup a custom field the user can change for uploads.

Within the PHP class there is a construct which sets up all the default options variables. I'm looking for the best way I can store this data so after the user sets a value it'll save as the new default upload folder. I'm thinking inside an external XML file, which is them imported into the PHP script.

Here's the upload class constructor function:

    function __construct($options=null) {
    $this->options = array(
        'script_url' => $this->getFullUrl().'/',
        'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/',

I hope this makes sense and I'm happy to clarify anything. But basically - how can I allow users to manually enter a directory structure and that becomes the new upload directory for images? Where could I save this new string variable? Probably not inside PHP but possibly in an external file, like XML or JSON?


回答1:


I noticed you and I posted a blueimp upload issue on github at about the same time. I'm not sure I have a complete answer for you, but let me give you what I've found so far. Maybe it will help:

I have been looking for a way to filter uploaded files by user from within an authenticated system. I posted github.com/blueimp/jQuery-File-Upload/issues/1578 . The plugin author suggested that I filter on the server side. I found github.com/blueimp/jQuery-File-Upload/issues/1149 which explains how to filter on the server side. I've done this and it works as far as uploading. I now have it setup where each user has a subfolder in the upload folder named their unique ID number which contains their images. So anyway, this is a dynamic way to set the upload path. Maybe you can use it.

My current problem is that although I can confirm that the images are uploaded they are not visible in the download table. Please see github.com/blueimp/jQuery-File-Upload/issues/1587 .

One attempt I've made to fix this included trying to pass options into the constructor:

Here is my code in the blueimp index.php file:

$customer_path_files = dirname($_SERVER['SCRIPT_FILENAME']) .     DIRECTORY_SEPARATOR.         'files' . DIRECTORY_SEPARATOR . $uid . DIRECTORY_SEPARATOR;
    if (!file_exists($customer_path_files)) {
    @mkdir($customer_path_files);
}
$customer_path_thumbnails = dirname($_SERVER['SCRIPT_FILENAME']) . DIRECTORY_SEPARATOR     . 'thumbnails' . DIRECTORY_SEPARATOR . $uid . DIRECTORY_SEPARATOR;
if (!file_exists($customer_path_thumbnails)) {
    @mkdir($customer_path_thumbnails);
}

$options=array(
'upload_dir' => $customer_path_files,
'upload_url' => base_url().'blueimp/server/php/files/'.$uid.'/',
'thumbnail' => array(
                'upload_dir' => $customer_path_thumbnails,
                'upload_url' => base_url().'blueimp/server/php/thumbnails/'.$uid.'/',
                'max_width' => 80,
                'max_height' => 80
            )
);


require('upload.class.php');

$upload_handler = new uploadHandler($options);

Hope something here helps,


Addendum:

I hope it does. BTW I read Jquery File Upload plugin: Dynamically change upload path?. That is exactly what I am trying to do. In case you are too, let me just say that I also tried to pass the session variable ( in my case the codeigniter variant ( $this->session->userdata('uid') ) in a hidden field to the plugin's index.php file, but just like in the post , it doesn't exist in index.php ( I think because , the upload button is not pushed for the repopulation or delete ). This is my first experience with ajax, but I think that the session ID must somehow be sent via Json. I'm trying to figure that out. It sounds like That's what Chris G did. If its any help I've posted the following question with some more details:

http://www.dynamicdrive.com/forums/showthread.php?p=279824




回答2:


My code for wordpress userdata,

Before:

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

    $this->options = array(
        'script_url' => $this->get_full_url().'/',
        'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/',
        'upload_url' => $this->get_full_url().'/files/',
        'user_dirs' => false,
        'mkdir_mode' => 0755,
        'param_name' => 'files',

After:

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

    require_once '../../../../wp-config.php';
    global $wpdb, $user_ID;

        if($user_ID) $userdi = $user_ID.'/';

    $this->options = array(
        'script_url' => $this->get_full_url().'/',
        'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/'.$userdi,
        'upload_url' => $this->get_full_url().'/files/'.$userdi,
        'user_dirs' => false,
        'mkdir_mode' => 0755,
        'param_name' => 'files',



回答3:


a couple of comments --

. These posts are from 2012/13. Later versions of jQuery File Upload have an option, 'user_dirs' => true, which insert a user identifier into the path where files are stored: (from UploadHander.php)

protected function get_upload_path($file_name = null, $version = null) {
    ...
    return $this->options['upload_dir'].$this->get_user_path().$file_name;
}

where get_user_path returns an empty string unless user_dirs is true.

. Rather than rewriting UploadHander.php with your preferred options, you can also pass options to the UploadHandler constructor -- in server/php/index.php, replace

$uploadHandler = new UploadHandler();

with

$uploadHandler = new UploadHandler(array('option1' => value,
                                         'option2' => value));


来源:https://stackoverflow.com/questions/11971753/custom-options-directory-for-jquery-blueimp-upload-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!