PHP:从服务器上的发布请求中保存图片

I receive picture from Java coder in bytes. It look like this in tcpdump(content of picture not full because of privacy):

......JFIF.....`.`.....C......
..
......(.....1#%.(:3=<9387@H\N@DWE78PmQW_bghg>Mqypdx\egc...C......./../cB8Bcccccccccccccccccccccccccccccccccccccccccccccccccc........p.."......................................6......................!..1.A."Qa2q...B....#4Ccr......................................................A.1............?..j*i.}.+vE..s...d85..5.....K..`..hc..y..=@.3.|N.<...?.......h....Y.$.5....W%....?E..cm0..$.$...I$...s...... Z.P2.3.j9..I..w\..F.......1.c.{.?\~..>.m4.......E......{.).nm"B.|....u..........[.H..>..4.~...k.pl......KO..;z/K..S4./.6i.....OpEc...'.m..0k9Y.r.......e. n........>...8.........~.X.R...h...f.."...q...../..mmn.<..\NI?2..q...O.2nd!......A=...\7.qZ.YS..,.+q.....

I need do save the picture on server, because I need to use it in shell script. How can I do it? Previously I did this: 1. First I went a little bit wrong way. I've saved request in file:

$query = file_get_contents("php://input");
$queryFile= fopen("/var/www/9292/querylog.txt","wb");
fwrite($queryFile, $query);
fclose($queryFile);

And when I open it with sublime text editor I see it in hexadecimal system. I decided that I should use this query and so I handle it this way:

$query = preg_replace('/[\s
]+/', '', $query);
$queryHex=pack("H*", $query);
if (preg_match("/Content-Length:[\s]+([0-9]+)([^|]*)\-\-\-\-[0-9]+boundary\-\-/", $queryHex))
{
    preg_match_all("/Content-Length:[\s]+([0-9]+)([^|]*)\-\-\-\-[0-9]+boundary\-\-/", $queryHex, $matches);
    $inputImage=$matches[2][0];
    $inputImage=trim($inputImage);
    $file=fopen("/var/www/9292/logs.tmp", "wb");
    fwrite($file, $matches[1][0]);
    fclose($file);
}
else
{
    $inputImage=$queryHex;
    $inputImage=trim($inputImage);
}
$imageFile=fopen("/var/www/9292/inputPhoto.jpg", 'wb');
fwrite($imageFile, $inputImage);
fclose($imageFile);

(It can contain Content-length or not). But where Java coder makes request, created picture was half-sized and not readable, however when I run this script with hexadecimal input, it works fine and creates his image. So if I understand it right, I should work with his request which I get in tcpdump. I tried some solutions, such as: 1. Just fwrite content in jpg file.(using parameter wb of course)
2. This func
3. Tried to make some other parametres of pack (C*,c*,a*,A*)
All that I tried created wrong picture, but I'm sure that it is possible to create and image from his bytes, because I create it from hexadecimal data. So, please help me. If I was on the right way? I should use some of functions I've tried, or something completely different? If you need something from my part ask me, I'll show it. Java coder doesn't want to show his code, but anyway I can ask him something concrete, I think he'll answer.

UPDATE Java coder says that he doesn't encode image.
UPDATE1 Tried base64 decoding

$query = file_get_contents("php://input");
    file_put_contents('/var/www/9292/querylog.txt', base64_decode($query));
    file_put_contents('/var/www/9292/query.log', base64_decode($query));
    file_put_contents('/var/www/9292/photo.jpg', base64_decode($query));

This way all files were created empty. If I try it simply without encoding:

$query = file_get_contents("php://input");
    file_put_contents('/var/www/9292/querylog.txt', $query);
    file_put_contents('/var/www/9292/query.log', $query);
    file_put_contents('/var/www/9292/photo.jpg', $query);

It creates jpg file with proper amount of bytes but it's still unreadable.

$file = fopen($filename, "w");
fputs($file, $query);
fclose($file);

Works perfect.