jQuery文件上传用户目录

时间:2017-02-02 13:44:26

标签: jquery upload customization

我尝试使用自己的目录以便将文件上传到它们中但是当我加载页面“upload_test.php”时出现错误

userPath is not defined

我已经尝试了很多解决方案2天但没有工作:(

以下是此页面的代码

<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,
));

1 个答案:

答案 0 :(得分:0)

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

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

您从PHP解释器收到NOTICE消息,因为无法保证userPath密钥始终存在。

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