dsa5233
2016-07-25 20:59
浏览 89
已采纳

用PHP获取Ajax头信息

I'm making an ajax function for uploading files (video or image) like this :

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

     }

And in my 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;
    ?>

My question is simple, how do I get the ajax header that I have set before in order to get the real filename of the file that I want to upload to server... Thanks in advance... :)

NOTE: I have tried to list all headers in $_SERVER as shown below :

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
odejs\;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

And from this list, I couldn't find any header that I have sent, like FILE_NAME, FILE_SIZE, and FILE_TYPE, so what's is the problem here...?? Thanks in advance again..

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanbei6427 2016-07-25 21:08
    已采纳

    Also, as of 5.4.0 you can use apache_request_headers(). It returns all HTTP request headers from the current request as array.

    In earlier versions this function works only if PHP installed as apache module.

    Usage will look like this:

    $headers = apache_request_headers();
    echo $headers['X-File-Name'];
    
    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题