Get size of POST-request in PHP(在 PHP 中获取 POST 请求的大小)
问题描述
有没有办法在 PHP 中获取 POST 请求正文的大小?
Is there any way to get size of POST-request body in PHP?
推荐答案
就这么简单:
$size = (int) $_SERVER['CONTENT_LENGTH'];
请注意,$_SERVER['CONTENT_LENGTH']
仅在 HTTP 请求方法为 POST(而非 GET)时设置.这是 Content-Length
标头的原始值,如 RFC 7230.
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-Length
header, as specified in RFC 7230.
在文件上传的情况下,如果你想获得上传文件的总大小,你应该遍历$_FILE
数组来总结每个$file['size']
.由于 POST 数据的编码开销,确切的总大小可能与原始 Content-Length
值不匹配.(另请注意,您应该使用每个 $_FILES
元素的 $file['error']
代码检查上传错误,例如 UPLOAD_ERR_PARTIAL
部分上传或 UPLOAD_ERR_NO_FILE
用于空上传.请参阅 文件上传错误 PHP 手册中的文档.)
In the case of file uploads, if you want to get the total size of uploaded files, you should iterate over the $_FILE
array to sum each $file['size']
. The exact total size might not match the raw Content-Length
value due to the encoding overhead of the POST data. (Also note you should check for upload errors using the $file['error']
code of each $_FILES
element, such as UPLOAD_ERR_PARTIAL
for partial uploads or UPLOAD_ERR_NO_FILE
for empty uploads. See file upload errors documentation in the PHP manual.)
这篇关于在 PHP 中获取 POST 请求的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中获取 POST 请求的大小


- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01