首页 > 解决方案 > PHP文件传输错误3

问题描述

我正在开发一个向我的网页发送“自制”HTTP 传输的应用程序。

"POST /file.php HTTP/1.0\r\n"
"Host: xxxx.com\r\n"
"Content-type: multipart/form-data; boundary=\"Abcdefghijklmnopqrstuvwxyz\"\r\n"
"Content-Length:163\r\n"
"\r\n"
"--Abcdefghijklmnopqrstuvwxyz\r\n"
"Content-Disposition: form-data; name=\"file\"; filename=\"myfile.txt\"; content-Type:text/plain\r\n"
"\r\n"
"test"
"--Abcdefghijklmnopqrstuvwxyz--\r\n"
"\r\n"

这是我的 HTTP POST 请求。看起来不错,但是当我执行请求并得到响应时,PHP 脚本文件指示文件上出现错误:“3”。意思是“UPLOAD_ERR_PARTIAL”,但我不知道为什么它表示这样的消息。有效负载长度与 Content-Length 值匹配。

你可以帮帮我吗 ?谢谢你。

标签: phpfilehttp

解决方案


The problem with your POST request is one missing CRLF in the boundary syntax.

The multipart/form-data is defined in RFC7578 and the boundary syntax is described in Section 4.1 as follows:

As with other multipart types, the parts are delimited with a boundary delimiter, constructed using CRLF, "--", and the value of the "boundary" parameter.

Let's examine your current POST request.

POST /file.php HTTP/1.0
Host: xxxx.com
Content-type: multipart/form-data; boundary="Abcdefghijklmnopqrstuvwxyz"
Content-Length: 163

--Abcdefghijklmnopqrstuvwxyz
Content-Disposition: form-data; name="file"; filename="myfile.txt"; content-Type:text/plain

test--Abcdefghijklmnopqrstuvwxyz--

Based on the specification, the last line should be two lines. There must always be a CRLF and -- before every boundary, so that last line should be like this:

test
--Abcdefghijklmnopqrstuvwxyz--

Therefore the acceptable POST request is this:

POST /file.php HTTP/1.0
Host: xxxx.com
Content-type: multipart/form-data; boundary="Abcdefghijklmnopqrstuvwxyz"
Content-Length: 165

--Abcdefghijklmnopqrstuvwxyz
Content-Disposition: form-data; name="file"; filename="myfile.txt"; content-Type:text/plain

test
--Abcdefghijklmnopqrstuvwxyz--

I'm calling it "acceptable" because there is actually another mistake but it is minor. My server simply ignores the minor mistake and accepts the request without a problem.


推荐阅读