POST已正确发送,但$ _POST和$ _FILES数组为空

时间:2019-05-17 13:23:29

标签: php http post

我正在尝试通过HTTP POST消息将图像从IP安全摄像机发送到Web服务器。尽管文件传输似乎正确完成,但是全局$ _FILES和$ _POST数组仍然为空。

服务器:适用于OS X 5.6.39,Apache 2.4.37的XAMPP
客户端:MOBOTIX IP凸轮(基于Linux,带有经过修改的软件包)

请注意,我无法控制客户端代码,因为我们正在谈论的是嵌入式系统,该系统已被制造商锁定,最重要的是,用于发送请求的二进制文件很可能是完全不是标准的(针对主机系统修改并重新编译了源代码)。

为解决此问题,我用更标准的工具(例如cURL)替换了IP摄像机,并且一切正常。

$ curl -v "http://localhost/test/test.php" -F "file=@pic.jpg"

这是预期的结果:

<pre>
POST dump:
array(0) {
}
FILES dump:
array(1) {
  ["file"]=>
  array(5) {
    ["name"]=>
    string(11) "pic.jpg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(45) "/Applications/XAMPP/xamppfiles/temp/phpz8TOrR"
    ["error"]=>
    int(0)
    ["size"]=>
    int(11706)
  }
}
</pre>

因此,我认为我的页面或Web服务器没有问题,但我想弄清楚是什么原因阻止了$ _FILES数组的填充。

这是我的PHP页面的代码:

<?php

sleep(2);

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if (!empty($_POST) || !empty($_FILES)) {

        echo "<pre>\nPOST dump:\n";
        var_dump($_POST);
        echo "FILES dump:\n";
        var_dump($_FILES);
        //print_r($_FILES);
        echo "</pre>\n";
        exit();

    } else {

        echo "Empty POST\n";
        exit();

    }

} else {

    echo "Only POST requests allowed\n";
    exit();

}   

?>

在IP摄像机上执行Wireshark跟踪的结果如下:

POST /test/test.php HTTP/1.0
Host: 192.168.1.100:80
User-Agent: mxmsg/2.2 (mx10-9-151-2; 10.9.151.2; MX-V4.4.1.55)
Accept: */ *
Content-Type: multipart/form-data; boundary="mxmsg_qswteervteuliaodpj9a7b5i3_20050405"

--mxmsg_qswteervteuliaodpj9a7b5i3_20050405
Content-Disposition: form-data; name="file"; filename="/tmp2/clipdir21301/E00000.jpg"
Content-Type: image/jpeg
Content-Length: 9256

<IMAGE DATA, less than 10 KB>

--mxmsg_qswteervteuliaodpj9a7b5i3_20050405--

HTTP/1.1 200 OK
Date: Fri, 17 May 2019 12:27:12 GMT
Server: Apache/2.4.37 (Unix) OpenSSL/1.0.2q PHP/5.6.39 mod_perl/2.0.8-dev Perl/v5.16.3
X-Powered-By: PHP/5.6.39
Content-Length: 11
Connection: close
Content-Type: text/html; charset=UTF-8

Empty POST

通过相机日志收到的其他信息:

INF: loading notify profile...
INF: set profile address "192.168.1.100:80"
INF: set profile http version: "HTTP/1.0"
INF: set profile http cgi path: "/test/test.php"
INF: set profile http authorize: ""
INF: using 'now' for base time
INF: add file '/tmp2/clipdir21301/E00000.jpg' to list
INF: [hdl#1] connection established (ip=192.168.1.100, port=80, fd=2) ...
INF: reached data limit of 512 bytes ... (stop reading)
INF: [hdl#1] HTTP-Headers: HTTP/1.1 200 OK
INF: [hdl#1] HTTP-Headers: Date: Fri, 17 May 2019 07:28:20 GMT
INF: [hdl#1] HTTP-Headers: Server: Apache/2.4.37 (Unix) OpenSSL/1.0.2q PHP/5.6.39 mod_perl/2.0.8-dev Perl/v5.16.3
INF: [hdl#1] HTTP-Headers: X-Powered-By: PHP/5.6.39
INF: [hdl#1] HTTP-Headers: Content-Length: 53
INF: [hdl#1] HTTP-Headers: Connection: close
INF: [hdl#1] HTTP-Headers: Content-Type: text/html; charset=UTF-8
INF: notification to 192.168.1.100:80 has been sent [msg okay=1].
INF: Server 192.168.1.100:80 response HTTP code 200.
Result:
INF: sending message succeed. 1x9256 bytes in 2.877s.

对cURL请求执行的Wireshark跟踪的结果:

POST /test/test.php HTTP/1.1
Host: 192.168.1.100
User-Agent: curl/7.64.1
Accept: */ *
Content-Length: 9486
Content-Type: multipart/form-data; boundary=------------------------7aa5e6b4427813b7
Expect: 100-continue

HTTP/1.1 100 Continue

--------------------------7aa5e6b4427813b7
Content-Disposition: form-data; name="file"; filename="pic.jpg"
Content-Type: image/jpeg

<IMAGE DATA, less than 10KB>

--------------------------7aa5e6b4427813b7--

HTTP/1.1 200 OK
Date: Fri, 17 May 2019 12:32:03 GMT
Server: Apache/2.4.37 (Unix) OpenSSL/1.0.2q PHP/5.6.39 mod_perl/2.0.8-dev Perl/v5.16.3
X-Powered-By: PHP/5.6.39
Content-Length: 323
Content-Type: text/html; charset=UTF-8

<pre>
POST dump:
array(0) {
}
<pre>
FILES dump:
array(1) {
  ["file"]=>
  array(5) {
    ["name"]=>
    string(11) "pic.jpg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(45) "/Applications/XAMPP/xamppfiles/temp/phptisqiC"
    ["error"]=>
    int(0)
    ["size"]=>
    int(9297)
  }
}
</pre>

任何提示将不胜感激!

[编辑]

我在另一台服务器(带有PHP 5.3的IIS 10)上进行了另一项测试;我使用了完全相同的PHP代码,但在这种情况下,服务器返回了411 Length Required错误消息,我认为这与照相机声明内容长度的方式有关。

POST /test/test.php HTTP/1.0
Host: 192.168.1.100:80
User-Agent: mxmsg/2.2 (mx10-9-151-2; 10.9.151.2; MX-V4.4.1.55)
Accept: */ *
Content-Type: multipart/form-data; boundary="mxmsg_qswteervteuliaodpj9a7b5i3_20050405"

--mxmsg_qswteervteuliaodpj9a7b5i3_20050405
Content-Disposition: form-data; name="file"; filename="/tmp2/clipdir21301/E00000.jpg"
Content-Type: image/jpeg
Content-Length: 9256

<IMAGE DATA, less than 10 KB>

--mxmsg_qswteervteuliaodpj9a7b5i3_20050405--

HTTP/1.1 411 Length Required
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 23 May 2019 09:22:32 GMT
Connection: close
Content-Length: 344

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Length Required</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Length Required</h2>
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>
</BODY></HTML>

根据RFC 2616,第4.4章消息长度...

  

如果请求包含消息正文,但Content-Length不包含   给定的情况下,服务器不能响应400(错误请求)   确定消息的长度,如果有则确定为411(需要长度)   它希望坚持接收有效的Content-Length。

...,并且上面报告的调试信息清楚地表明,相机包括附加图像的长度,但不是整个请求中的一个。现在的问题是:为什么XAMPP(Apache)返回200 OK,而ISS返回411 Length Required?

0 个答案:

没有答案