【问题标题】:jQuery file upload user directoriesjQuery文件上传用户目录
【发布时间】:2017-02-02 13:44:26
【问题描述】:

我尝试使用自己的目录将文件上传到其中,但在加载页面“upload_test.php”时出现错误

userPath is not defined

这两天我尝试了很多解决方案,但都没有奏效:(

这是我的页面代码

<form id="fileupload" action="UploadHandler.php?userPath=<?php echo $MYDIR;?>" method="POST" enctype="multipart/form-data">

这是我在 main.js 中的代码

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

对于 index.php

error_reporting(E_ALL | E_STRICT);

require('UploadHandler.php');

class CustomUploadHandler extends UploadHandler {

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

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)
    .'&userPath='.$_REQUEST['userPath']
    ;
  $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,
));

【问题讨论】:

    标签: jquery upload customization


    【解决方案1】:

    我会修改 get_user_id() 函数如下:

    protected function get_user_id() {
        return array_key_exists('userPath', $_REQUEST) ? $_REQUEST['userPath'] : '';
    }
    

    您会从 PHP 解释器收到一条 NOTICE 消息,因为不能保证 userPath 键始终存在。

    每次与数组交互时,例如$_REQUEST 数组,您需要准备您的应用程序来处理特定键不存在的情况,否则您将收到一条 NOTICE 消息,就像您在上面得到的那样。

    【讨论】:

    • 感谢您的回答。确实,效果更好!但是,上传的文件不会保存到我的自定义文件夹中,只有文件文件夹下的文件可见。我认为 UploadHandler 忽略了我的自定义文件夹。
    • 您能否检查以下内容: - 文件夹是否具有正确的文件权限? - 文件夹是否存在您要放置文件的位置? - 您是否有权创建新文件夹?
    • 这边一切都很好。我的文件夹已创建并且权限正常。我尝试在 main.js 中更改它以测试: url: 'file_upload/server/php/index.php?userPath=MYDIR' 并且它有效。但我想使用一个参数(当然!)
    猜你喜欢
    • 1970-01-01
    • 2017-04-14
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 2016-01-16
    • 2014-08-17
    相关资源
    最近更新 更多