在 PHP 中获取 POST 请求的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1361451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get size of POST-request in PHP
提问by Andrey M.
Is there any way to get size of POST-request body in PHP?
有没有办法在 PHP 中获取 POST 请求正文的大小?
回答by scoffey
As simple as:
就这么简单:
$size = (int) $_SERVER['CONTENT_LENGTH'];
Note that $_SERVER['CONTENT_LENGTH']is only set when the HTTP request method is POST (not GET). This is the raw value of the Content-Lengthheader, as specified in RFC 7230.
请注意,$_SERVER['CONTENT_LENGTH']仅在 HTTP 请求方法为 POST(而非 GET)时设置。这是Content-Length标头的原始值,如RFC 7230 中所指定。
In the case of file uploads, if you want to get the total size of uploaded files, you should iterate over the $_FILEarray to sum each $file['size']. The exact total size might not match the raw Content-Lengthvalue due to the encoding overhead of the POST data. (Also note you should check for upload errors using the $file['error']code of each $_FILESelement, such as UPLOAD_ERR_PARTIALfor partial uploads or UPLOAD_ERR_NO_FILEfor empty uploads. See file upload errorsdocumentation in the PHP manual.)
在文件上传的情况下,如果您想获得上传文件的总大小,您应该遍历$_FILE数组以求和每个$file['size']. Content-Length由于 POST 数据的编码开销,确切的总大小可能与原始值不匹配。(另请注意,您应该使用$file['error']每个$_FILES元素的代码检查上传错误,例如UPLOAD_ERR_PARTIAL部分上传或UPLOAD_ERR_NO_FILE空上传。请参阅PHP 手册中的文件上传错误文档。)
回答by Michael Krelin - hacker
My guess is, it's in the $_SERVER['CONTENT_LENGTH'].
我的猜测是,它在$_SERVER['CONTENT_LENGTH'].
And if you need that for error detection, peek into $_FILES['filename']['error'].
如果您需要它进行错误检测,请查看$_FILES['filename']['error'].
回答by Keith Palmer Jr.
If you're trying to figure out whether or not a file upload failed, you should be using the PHP file error handling as shown at the link below. This is the most reliable way to detect file upload errors:
http://us3.php.net/manual/en/features.file-upload.errors.php
如果您试图确定文件上传是否失败,您应该使用 PHP 文件错误处理,如下面的链接所示。这是检测文件上传错误的最可靠方法:http:
//us3.php.net/manual/en/features.file-upload.errors.php
If you need the size of a POST request without any file uploads, you should be able to do so with something like this:
如果您需要没有任何文件上传的 POST 请求的大小,您应该可以这样做:
$request = http_build_query($_POST);
$size = strlen($request);
回答by p4bl0
This might work :
这可能有效:
$bytesInPostRequestBody = strlen(file_get_contents('php://input'));
// This does not count the bytes of the request's headers on its body.
回答by n1313
I guess you are looking for $HTTP_RAW_POST_DATA
我猜你正在寻找 $HTTP_RAW_POST_DATA

