Domanda

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!

È stato utile?

Soluzione

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...

Altri suggerimenti

I managed to perform the task by adding image binary directly to post_data file.

cat image.jpeg >> post_data

The content of post_data file looks like this.

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

"binary content goes here"
--123456789

And make sure that your post_data has CRLF file ending instead of LF.

Short answer:

Adding Content-Transfer-Encoding: base64 solved this.


Full answer:

Full post-data.txt file, with CRLF endings:

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

[base64 encoded image here]

--1234567890--

Full command:

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

Error origin:

Encountered this error while using Flask with imageio.imread(file) which resulted in error Could not find a format to read the specified file in mode 'i'

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top