Search code examples
phppostbase64image-uploadingapachebench

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


I'm using ApacheBench to benchmark a PHP image upload module. Thing is, when I dump $_FILES["my_file"] I can see that the temp file stored by PHP is not an image file, it's a base64 (text/plain) file. Shouldn't PHP be storing that file as an image file, given that the POST request is telling it that the uploaded file's content type is image/jpeg? Or is PHP behaving as expected and it's my job to handle the binary data inside of $_FILES["my_file"]["tmp_name"]?

Here's how I'm running ab:

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

Here's the contents of /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--

[Note that I tried removing "Content-Type: text/plain" but it seems to make no difference]

Thanks!


Solution

  • I've finally given up on this one: PHP ignores the header (Content-Transfer-Encoding) and I end up with a text file containing a huge string of binary data instead of an image file.

    Not AB's fault...