Mark
Mark

Reputation: 69920

POST Base64 encoded data in PHP

I need to POST some data to a PHP page using cURL, and the request contains three parameters:

I've noticed that the Base64 value is corrupted during the transmission.

This is the code that's sending the request:

$filename = "img2.jpg"; //A sample image file
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$base64 = base64_encode($data);

$postData = "id=1234&sometext=asdasd&data=" . $base64;

$ch = curl_init("http://mydomain/post.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$httpResponse = curl_exec($ch);
curl_close($ch);

Any tips?

Upvotes: 3

Views: 18280

Answers (3)

Teson
Teson

Reputation: 6736

A fair guess is that the encoding adds + - signs, which mess up your data.

After encode, try to add replace + to - (And backwards on recieve of course.)

Ref: http://en.wikipedia.org/wiki/Base64#URL_applications

Upvotes: 0

castis
castis

Reputation: 8223

Make sure that the size of the post data does not exceed your 'max_post_size' in your php.ini file.

Upvotes: 2

MatTheCat
MatTheCat

Reputation: 18721

Maybe you should use urlencode() because the + and = in a base64 string?

Upvotes: 15

Related Questions