PHP Jquery文件上传//用户名/会话目录 [英] PHP Jquery File Upload // Username/Session Directories

查看:354
本文介绍了PHP Jquery文件上传//用户名/会话目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在PHP上使用Blueimp的jquery文件上传几个星期,我遇到了一些问题。我正在自学自己如何用PHP进行编程,所以我仍然习惯于如何工作,所以对于任何困惑都表示歉意,我会尽力澄清。

我需要将用户上传的内容插入上传目标目录中的特定用户名文件夹中。我已经跟着众多的书面,答案和常见问题打开所说的用户目录(对于这个插件),最好的情况下,一个文件夹是用一串misc创建的会话。字符,但所有上传,无论用户名转到相同的文件夹,而不是用户的各自的文件夹。是否可以告诉脚本读取和使用特定的用户名/ ID / etc作为变量?我很确定这是,但目前我找不到一个明确的方法。为了清楚起见,我尽量保持我的代码基本和类似于发布的Blueimp版本。



所以,这里就是这样。



**工作:**用户注册,info被插入数据库,上传目录中会自动创建一个相应的用户名文件夹,并耐心地等待美味的文件。



**不工作:**上传脚本不读取用户名/变量被用作目标文件夹名称。当执行时,Error SyntaxError:意外的标记<返回,并将上传的文件插入父/ uploads /文件夹。

index.php(原始服务器/ php /目录内)

  error_reporting(E_ALL | E_STRICT); 
require('UploadHandler.php');
$ upload_handler = new UploadHandler();

UploadHandler.php

 函数__construct($ options = null,$ initialize = true,$ error_messages = null){
$ this-> response = array();
$ this-> options = array(
'script_url'=> $ this-> get_full_url()。'/'。basename($ this-> get_server_var('SCRIPT_NAME')) ,
'upload_dir'=> dirname($ this-> get_server_var('SCRIPT_FILENAME'))。'/ .. / .. / uploads /'.$ username。'/',
' '$','$','$','$','$','$'和'$' mkdir_mode'=> 0755,
'param_name'=>'files',

我的理解是,$ username变量需要被定义,但是我在哪里以及怎样丢失了我可以进行页面调用,并且在页面上打印用户名,ID,电子邮件以及其他任何东西,但是变量/标记没有翻译成上传目录脚本。



预先感谢您的任何输入。

解决方案

你可以尝试恢复原来的服务器/ php / UploadHandler.php然后按照这篇文章在wiki:



jQuery-File-Upload wiki - PHP用户目录 你可以重写get_user_id方法



编辑你的index.php:

 <?php 

error_reporting(E_ALL | E_STRICT);

require('UploadHandler.php');
$ b $ class CustomUploadHandler extends UploadHandler {
$ b $ protected function get_user_id(){
return $ _REQUEST ['username'];

$ b保护函数set_additional_file_properties($ file){
$ file-> deleteUrl = $ this-> options ['script_url']
。$ this - > get_query_separator($ this-> options ['script_url'])
。$ this-> get_singular_param_name()
。'='。rawurlencode($ file-> name)
。'& username ='。$ _ REQUEST ['username']
;
$ file-> deleteType = $ this-> options ['delete_type'];
if($ file-> deleteType!=='DELETE'){
$ file-> deleteUrl。='& _method = DELETE';

if($ this-> options ['access_control_allow_credentials']){
$ file-> deleteWithCredentials = true;




$ upload_handler = new CustomUploadHandler(array(
'user_dirs'=> true,
));

和main.js

  $(function(){
'use strict';

//初始化jQuery文件上传部件:
$('#fileupload') .fileupload({
//取消注释以下内容以发送跨域cookie:
// xhrFields:{withCredentials:true},
url:'server / php / index.php?username ='+ username,
});

//通过重定向选项启用iframe跨域访问:
$('#fileupload')。fileupload(
'选项',
'重定向',
window.location.href.replace(
/ \ / [^ \ /] * $ /,
'/ cors / result.html?%s'

);

//加载现有文件:
$('#fileupload')。addClass('fileupload-processing ');
$ .ajax({
//取消注释以下内容以发送跨域cookie:
// xhrFields:{withCredenti ('#fileupload')。fileupload('option','url'),
dataType:'json',
context:$('#' fileupload')[0]
})。always(function(){
$(this).removeClass('fileupload-processing');
))。done(function(result){
$(this).fileupload('option','done')
.call(this,$ .Event('done')) ,{result:result});
});

});

}


I have been messing around with Blueimp's Jquery File Upload for a couple weeks on PHP and I am encountering some issues. I am teaching myself how to program in PHP so I am still getting acclimated to how things work, so apologies for any confusion and I will attempt to clarify as best I can.

I need user uploads to be inserted into their specific username folders in the upload destination directory. I have followed the numerous writeups, answers and faqs for turning on said user directories (for this plugin) and at best, a folder is created for the session with a string of misc. characters, but all uploads regardless of the username go to the same folder, not the users' respective folder. Is it possible to tell the script to read and use the specific username/id/etc as a variable? I'm pretty sure it is, but currently I cannot find a clear-cut method. I have tried to keep my code as basic and similar to the released Blueimp versions for the sake of clarity.

So, here goes.

**Working:**User registers, info is inserted into database, a respective username folder is automatically created inside the upload directory and waits patiently to be filled with delicious files.

**Not working:**Upload script is not reading the username/variable to be used as the target folder name. When executed, "Error SyntaxError: Unexpected token <" is returned, and inserts the uploaded file into the parent "/uploads/" folder.

index.php (inside the original server/php/ directory)

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');
$upload_handler = new UploadHandler();

UploadHandler.php

function __construct($options = null, $initialize = true, $error_messages = null) {
    $this->response = array();
    $this->options = array(
        'script_url' => $this->get_full_url().'/'.basename($this->get_server_var('SCRIPT_NAME')),
        'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/../../uploads/'.$username.'/',
        'upload_url' => $this->get_full_url().'/../../uploads/'.$username.'/',
        'user_dirs' => false,
        'mkdir_mode' => 0755,
        'param_name' => 'files',

My understanding is that the $username variable needs to be defined, but where and how is where I'm lost. I can have the page call and print the username, id, email, and pretty much anything else -on- the page, but the variable/token is not translating into the upload directory script.

Thanks in advance for any input.

解决方案

You can try to restore the original server/php/UploadHandler.php then follow this article in the wiki:

jQuery-File-Upload wiki - PHP-user-directories

"To provide your own implementation, you can override the get_user_id method"

edit your index.php:

<?php

error_reporting(E_ALL | E_STRICT);

require('UploadHandler.php');

class CustomUploadHandler extends UploadHandler {

    protected function get_user_id() {
        return $_REQUEST['username'];
    }

    protected function set_additional_file_properties($file) {
        $file->deleteUrl = $this->options['script_url']
            .$this->get_query_separator($this->options['script_url'])
            .$this->get_singular_param_name()
            .'='.rawurlencode($file->name)
            .'&username='.$_REQUEST['username']
            ;
        $file->deleteType = $this->options['delete_type'];
        if ($file->deleteType !== 'DELETE') {
            $file->deleteUrl .= '&_method=DELETE';
        }
        if ($this->options['access_control_allow_credentials']) {
            $file->deleteWithCredentials = true;
        }
    }   

}

$upload_handler = new CustomUploadHandler(array(
    'user_dirs' => true,
));

and main.js

$(function () {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload({
        // Uncomment the following to send cross-domain cookies:
        //xhrFields: {withCredentials: true},
        url: 'server/php/index.php?username=' + username,
    });

    // Enable iframe cross-domain access via redirect option:
    $('#fileupload').fileupload(
        'option',
        'redirect',
        window.location.href.replace(
            /\/[^\/]*$/,
            '/cors/result.html?%s'
        )
    );

        // Load existing files:
        $('#fileupload').addClass('fileupload-processing');
        $.ajax({
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true},
            url: $('#fileupload').fileupload('option', 'url'),
            dataType: 'json',
            context: $('#fileupload')[0]
        }).always(function () {
            $(this).removeClass('fileupload-processing');
        }).done(function (result) {
            $(this).fileupload('option', 'done')
                .call(this, $.Event('done'), {result: result});
        });

});

}

这篇关于PHP Jquery文件上传//用户名/会话目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆