2

我正在使用 ApacheBench 对 PHP 图像上传模块进行基准测试。问题是,当我转储时,$_FILES["my_file"]我可以看到 PHP 存储的临时文件不是图像文件,而是 base64(文本/纯文本)文件。鉴于POST请求告诉它上传文件的内容类型是,PHP 不应该将该文件存储为图像文件image/jpeg吗?或者 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",但似乎没有任何区别]

谢谢!

4

3 回答 3

1

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

不是AB的错...

于 2013-11-01T15:33:06.513 回答
0

我设法通过将图像二进制文件直接添加到 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。

于 2019-04-12T03:13:46.897 回答
0

简短的回答:

添加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

错误来源:

使用 Flask 时遇到此错误imageio.imread(file)导致错误Could not find a format to read the specified file in mode 'i'

于 2019-12-19T17:14:31.460 回答