View Full Version : QHttpMultiPart - sending image to web serwer question

18th January 2013, 16:13

i'am trying to post image to web server using QHttpMultiPart, and ewerything was fine until i moved my php part to new web server... now images i'am saving have only 4bytes and when i try to open them in browser it says that the file is corrupted. What can be the couse of such behaving ?

on request finished replay var_dump($_POST) gives me this:

array(3) {
string(2) "10"
string(9) "test1.jpg"
string(4) "˙Ř˙ŕ" << here is the problem, length is only 4

but when i post to old server i get

array(3) {
string(2) "10"
string(9) "test1.jpg"
string(112871) "˙Ř˙ŕ << here length is ok

in wireshark post looks fine for me. Code i'am using is taken form QHttpMultiPart class reference, so it's pretty basic.
thanks for any help and sorry for my poor english.

18th January 2013, 21:30
Nothing to do with Qt.

I will take a guess: the fifth byte in the original file is a zero byte and this is being treated as a string terminator. Does the binary part show up in the PHP _FILES global?


if(!$_FILES) {
echo "No file!"

$id = $_FILES['image']['name'];
move_uploaded_file($_FILES['image']['tmp_name'], "/permanent/location".$id);

18th January 2013, 21:58
hmm i think i checked $_FILES and it was NULL, but i'am gonna check again tomorrow.

and it's normal that in about 10 files i checked fifth byte is 0 ?


19th January 2013, 00:01
You said the bytes are being sent correctly so the problem is in how PHP interprets them.

I guessed a zero byte because because that's a common side effect of treating binary as text. Your example is a JPEG mage. JPEG images start with the bytes 0xFF 0xD8 0xFF 0xE0 followed by two byte block length (big-end first) and the four letters JFIF etc. In the case the header block is small (usually) the fifth byte of the file will be 0. The characters you see are what I would expect if PHP were treating the bytes as characters encoded according to the ISO/IEC 8859-2:1999 (Latin-2) or Windows CP1250 (Eastern Europe) 8-bit encoding.

19th January 2013, 00:31
hm i get it i think but still, don't know how to fix it :P
php file that receives post is in utf-8, qt sends file probably in cp1250 as i'am polish.

20th January 2013, 04:48
You should use the _FILES super global to access binary uploaded files from a temporary directory on the server
encode the binary data as Base64 (or some other text only encoding) for use in the _POST variable.

See http://php.net/manual/en/features.file-upload.php

To have the file appear in _FILES modify the doc example:

QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHe ader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispos itionHeader, QVariant("form-data; name=\"image\"; filename=\"image.jpg\"")); // <<< filename triggers separate handling
QFile *file = new QFile("image.jpg");
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

and then you will see in the PHP environment:

chrisw@newton /tmp/simple_example $ ./simple_example
// _POST array
array(1) {
string(7) "my text"
// _FILES array
array(1) {
array(5) {
string(9) "image.jpg"
string(10) "image/jpeg"
string(14) "/tmp/phpaLYWWk"

20th January 2013, 20:59
ok, thanks for help, i'am going to try it tomorrow at work and see how it works :)