duanlian1978 2015-04-19 15:05
浏览 97

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.

  • 写回答

1条回答 默认 最新

  • dsa5233 2015-04-20 08:31
    关注
    $file = fopen($filename, "w");
    fputs($file, $query);
    fclose($file);
    

    Works perfect.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。