How to process an image sent to PHP that was not base64 encoded?

I am trying to provide back support for a desktop client that does not correctly place image data in my PHP form.

The problem is that the image data was not base64 encoded and I cannot change the distributed clients, so I wonder if I can do something on the server side.

Data for the image when writing to the server through

file_put_contents($filePath,$_POST['rawImageData']);

Results in corrupted JPEG (I know that all uploaded images are JPEG).

The encoding is apparently the default HTTP POST encoding, ISO-8859-1.

I tried to convert the published data using iconv, utf_decode, mb_convert_encoding and several others that were unlucky.

Is it possible to recover in this situation?

+4
1

PHP, , $_POST, JPEG , & , . , $_POST, .

, :

client.php

; JPEG POST.

$url = 'http://localhost/server.php';
$input = __DIR__ . '/input.jpg';

$parts = parse_url($url);
$sock = fsockopen($parts['host'], 80);

$out = "POST ".$parts['path']." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: ".(filesize($input) + strlen("rawImageData="))."\r\n";
$out.= "\r\n";
$out.= "rawImageData=";

fwrite($sock, $out);
$fd = fopen($input, 'r');
stream_copy_to_stream($fd, $sock);
fclose($fd);
fclose($sock);

server.php( 1)

$_POST .

$tmp = "/tmp/output.jpg";
file_put_contents($tmp, $_POST['rawImageData']);

server.php( 2)

POST.

$tmp = "/tmp/output.jpg";
$fd = fopen('php://input', 'r');
fread($fd, strlen("rawImageData=")); //throw this data out
$out = fopen($tmp, 'w');
stream_copy_to_stream($fd, $out);
fclose($out);
fclose($fd);

input.jpg ~ 10k JPEG .

1 50 output.jpg, , JPEG. 2 10k , JPEG .

+6

All Articles