ab(ApacheBench)图像上传和PHP$_FILES临时文件保存为base64文本


ab (ApacheBench) image upload and PHP $_FILES - temp file saved as base64 text

我正在使用ApacheBench对PHP图像上传模块进行基准测试。问题是,当我转储$_FILES["my_file"]时,我可以看到PHP存储的临时文件不是图像文件,而是base64(text/plain)文件。考虑到POST请求告诉它上传的文件的内容类型是image/jpeg,PHP不应该将该文件存储为图像文件吗?或者PHP的行为是否如预期,而我的工作是处理$_FILES["my_file"]["tmp_name"]中的二进制数据?

以下是我运行ab:的方式

$>ab -v 4 -n 10 -c 2 -p /home/post_data.txt -T "multipart/form-data;'
boundary=1234567890" http://localhost/image_upload

以下是/home/post_data.txt的内容:

--1234567890
Content-Disposition: form-data; name="token"
Content-Type: text/plain
1
--1234567890
Content-Disposition: form-data; name="text"
Content-Type: text/plain
Testing
--1234567890
Content-Disposition: form-data; name="status"
Content-Type: text/plain
1
--1234567890
Content-Disposition: form-data; name="uploaded_file"; filename="my_image.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
[[base64 image data]]
--1234567890--

[注意,我尝试删除"Content-Type: text/plain",但似乎没有什么区别]

谢谢!

我终于放弃了这一点:PHP忽略了头(Content-Transfer-Encoding),我最终得到了一个包含大量二进制数据的文本文件,而不是一个图像文件。

不是AB的错。。。

我通过将图像二进制直接添加到post_data文件来执行任务。

cat image.jpeg >> post_data

post_data文件的内容如下所示。

--123456789
Content-Disposition: form-data; name="file"; filename="image.jpg"
Content-Type: image/jpeg
"binary content goes here"
--123456789

并确保您的post_data的结尾是CRLF文件而不是LF。

简短回答:

添加Content-Transfer-Encoding: base64解决了这个问题。


完整答案:

完整的post-data.txt文件,以CRLF结尾:

--1234567890
Content-Disposition: form-data; name="file"; filename="image.jpg"
Content-Transfer-Encoding: base64
Content-Type: image/jpeg
[base64 encoded image here]
--1234567890--

完整命令:

$ ab -c 5 -n 5 -p ./post_data.txt -T "multipart/form-data; boundary=1234567890" http://localhost:8080/upload

错误来源:

将烧瓶与imageio.imread(file)一起使用时遇到此错误,导致错误Could not find a format to read the specified file in mode 'i'