gpt4 book ai didi

php - 在 PHP 中获取 Ajax 头信息

转载 作者:行者123 更新时间:2023-12-04 14:41:51 26 4
gpt4 key购买 nike

我正在制作一个 ajax 函数来上传文件(视频或图像),如下所示:

function upload_file(file)
{
//create xhr object
xhr = new XMLHttpRequest();

//initiate request
xhr.open('post','hisoka_drop.php',true);//true for asynchronous

//set headers
xhr.setRequestHeader('Content-Type',"multipart/form-data");
xhr.setRequestHeader('X-File-Name',file.fileName);
xhr.setRequestHeader('X-File-Size',file.fileSize);
xhr.setRequestHeader('X-File-Type',file.fileType);

//send the file
xhr.send(file);

}

在我的 hisoka_drop.php 中:
<?php
$str =file_get_contents('php://input');
$filename = ; //How to get ajax header about file.fileName;
$path = 'upload/'.$filename;
file_put_contents($path,$str);
echo $path;
?>

我的问题很简单,如何获取我之前设置的 ajax header ,以便获取我要上传到服务器的文件的真实文件名...
提前致谢... :)

笔记:
我试图列出 $_SERVER 中的所有标题,如下所示:
Key ==>MIBDIRS
Value ==>C:/xampp/php/extras/mibs
Key ==>MYSQL_HOME
Value ==>\xampp\mysql\bin
Key ==>OPENSSL_CONF
Value ==>C:/xampp/apache/bin/openssl.cnf
Key ==>PHP_PEAR_SYSCONF_DIR
Value ==>\xampp\php
Key ==>PHPRC
Value ==>\xampp\php
Key ==>TMP
Value ==>\xampp\tmp
Key ==>HTTP_HOST
Value ==>localhost:8080
Key ==>HTTP_CONNECTION
Value ==>keep-alive
Key ==>CONTENT_LENGTH
Value ==>305658
Key ==>HTTP_ORIGIN
Value ==>http://localhost:8080
Key ==>HTTP_USER_AGENT
Value ==>Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36
Key ==>CONTENT_TYPE
Value ==>multipart/form-data
Key ==>HTTP_ACCEPT
Value ==>*/*
Key ==>HTTP_DNT
Value ==>1
Key ==>HTTP_REFERER
Value ==>http://localhost:8080/task3/
Key ==>HTTP_ACCEPT_ENCODING
Value ==>gzip, deflate
Key ==>HTTP_ACCEPT_LANGUAGE
Value ==>en-US,en;q=0.8,id;q=0.6
Key ==>HTTP_COOKIE
Value ==>PHPSESSID=crk3e9v9m4ri6k2nn8snhihu67
Key ==>PATH
Value ==>C:\Python33\;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\TortoiseGit\bin;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\nodejs\;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;
Key ==>SystemRoot
Value ==>C:\Windows
Key ==>COMSPEC
Value ==>C:\Windows\system32\cmd.exe
Key ==>PATHEXT
Value ==>.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Key ==>WINDIR
Value ==>C:\Windows
Key ==>SERVER_SIGNATURE
Value ==><address>Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.5.24 Server at localhost Port 8080</address>

Key ==>SERVER_SOFTWARE
Value ==>Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.5.24
Key ==>SERVER_NAME
Value ==>localhost
Key ==>SERVER_ADDR
Value ==>::1
Key ==>SERVER_PORT
Value ==>8080
Key ==>REMOTE_ADDR
Value ==>::1
Key ==>DOCUMENT_ROOT
Value ==>C:/xampp/htdocs
Key ==>REQUEST_SCHEME
Value ==>http
Key ==>CONTEXT_PREFIX
Value ==>
Key ==>CONTEXT_DOCUMENT_ROOT
Value ==>C:/xampp/htdocs
Key ==>SERVER_ADMIN
Value ==>postmaster@localhost
Key ==>SCRIPT_FILENAME
Value ==>C:/xampp/htdocs/task3/hisoka_drop.php
Key ==>REMOTE_PORT
Value ==>60706
Key ==>GATEWAY_INTERFACE
Value ==>CGI/1.1
Key ==>SERVER_PROTOCOL
Value ==>HTTP/1.1
Key ==>REQUEST_METHOD
Value ==>POST
Key ==>QUERY_STRING
Value ==>
Key ==>REQUEST_URI
Value ==>/task3/hisoka_drop.php
Key ==>SCRIPT_NAME
Value ==>/task3/hisoka_drop.php
Key ==>PHP_SELF
Value ==>/task3/hisoka_drop.php
Key ==>REQUEST_TIME_FLOAT
Value ==>1469489572.122
Key ==>REQUEST_TIME
Value ==>1469489572
Key ==>REQUEST_TIME

从这个列表中,我找不到我发送的任何标题,如 FILE_NAME、FILE_SIZE 和 FILE_TYPE,那么这里有什么问题......?
再次提前致谢..

最佳答案

此外,从 5.4.0 开始,您可以使用 apache_request_headers() .它将当前请求中的所有 HTTP 请求 header 作为数组返回。

在早期版本中,此功能仅在 PHP 安装为 apache 模块时才有效。

用法如下所示:

$headers = apache_request_headers();
echo $headers['X-File-Name'];

关于php - 在 PHP 中获取 Ajax 头信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38577338/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com