doucao1888 2013-04-05 21:06
浏览 51

jquery-file-upload插件:我可以在运行时设置上传路径吗?

I've found other posts that talk about changing the upload path for the jquery-file-upload plugin. However, the answers usually involve hardcoding it into the upload.class.php file.

I know how to do this, but the thing is, there will be multiple users uploading multiple files that need to go to different folders. I need to be able to dynamically set the destination path of the files.

Is there any way this can be done? I've gone over the jquery-file-upload documentation and there's no mention of such an option. In fact, maybe it's just me being tired, but the documentation seems to be very lacking anyway.

If there's no way to dynamically set the upload path, then one idea I have for a workaround would be to run another script after the upload is complete that will move the files from the hardcoded destination to their proper location, but it just seems silly to have to do that.

  • 写回答

2条回答 默认 最新

  • dongzg2006 2013-04-10 04:07
    关注

    Here's the solution I finally ended up with.

    First, I added URL parameters to the data-url in the input tag that the file uploader is attached to (it should be pointing to the jquery file upload server/php/ subfolder, which contains an index.php file):

    <input type="file" name="files[]" data-url="/jquery-file-upload/server/php/?type=a">
    

    I then used a SWITCH statement in the index.php file that the data-url points to:

       switch ($_GET["type"]) {
    
        case "a":
            $upload_dir = '/custom/folder/here/';
            $upload_url = '/custom/folder/here';
            $allowed_exts = '/\.(jpg|jpeg|gif|png)$/i';
            break;
    
        case "b":
            // you get the idea
            break;
    
        default:
            $upload_dir = dirname($_SERVER['SCRIPT_FILENAME']).'/files/';
            $allowed_exts = '/.+$/i';
            break;
    }   
    

    Finally, in the upload.class.php file I modified the appropriate lines depending on which values I wanted to customize using the SWITCH:

        function __construct($options = null, $initialize = true) {
            $this->options = array(
            'script_url' => $this->get_full_url().'/',
            'upload_dir' => $GLOBALS["upload_dir"],
            'upload_url' => $GLOBALS["upload_url"],
            'user_dirs' => false,
            'mkdir_mode' => 0755,
            'param_name' => 'files',
            'delete_type' => 'DELETE',
            'access_control_allow_origin' => '*',
            'download_via_php' => false,
            'inline_file_types' => '/\.(gif|jpe?g|png)$/i',
            'accept_file_types' => $GLOBALS["allowed_exts"],
            ...etc
    

    The variables that I defined in the SWITCH are used here as `$GLOBALS["varname"] so that they can be used from within the class.

    I hope this is able to help someone. Each person's case is going to be different so I don't recommend copying and pasting this code exactly. I was just trying to illustrate the basic idea.

    NOTE: "upload_dir" is the ROOT path and "upload_url" is the public path.

    Also note: As you can see in my example I also used this method to determine which filetypes to allow depending on the conditions.

    There is probably a built-in way to do this through the file uploader API/Options, but the documentation is very vague about what you can actually do.

    评论

报告相同问题?

悬赏问题

  • ¥15 计算二重积分∫∫e^(x+y)dxdy,其中0≤x≤1,0≤y≤1,试分别用复合辛普森公式(取n=4)以及高斯求积公式(取n=4)计算积分 给出matlab程序
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的