PDA

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



unnamed
18th January 2013, 15:13
Hi,

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) {
["idx"]=>
string(2) "10"
["qname"]=>
string(9) "test1.jpg"
["qfile"]=>
string(4) "˙Ř˙ŕ" << here is the problem, length is only 4
}

but when i post to old server i get



array(3) {
["idx"]=>
string(2) "10"
["qname"]=>
string(9) "test1.jpg"
["qfile"]=>
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.

ChrisW67
18th January 2013, 20: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?


<?php

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

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

unnamed
18th January 2013, 20: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 ?

hmmm

ChrisW67
18th January 2013, 23: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.

unnamed
18th January 2013, 23: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.

ChrisW67
20th January 2013, 03:48
You should use the _FILES super global to access binary uploaded files from a temporary directory on the server
OR
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->open(QIODevice::ReadOnly);
imagePart.setBodyDevice(file);
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) {
["text"]=>
string(7) "my text"
}
// _FILES array
array(1) {
["image"]=>
array(5) {
["name"]=>
string(9) "image.jpg"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(14) "/tmp/phpaLYWWk"
["error"]=>
int(0)
["size"]=>
int(16246)
}
}

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