本文介绍了获取PHP中POST请求的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以获取PHP中POST请求主体的大小?

Is there any way to get size of POST-request body in PHP?

推荐答案

很简单:

$size = (int) $_SERVER['CONTENT_LENGTH'];

请注意,仅当HTTP请求方法为POST(而非GET)时才设置$_SERVER['CONTENT_LENGTH'].这是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请求的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:34