views:

1051

answers:

4

I'm using AS3 to base64 encode a JPG and pass it to Javascript. I'm attempting to use AJAX to pass that base64 encoded string to PHP and have PHP decode it. (I know I could send it back to Flash and Flash could decode it and send it to PHP, but I'm trying to eliminate the need for Flash on the decoding end).

It appears that AS3's encodeToBase64String() function and and PHP's base64_decode() function do not use the same algorithm, as PHP evaluates it as a base64 encoded object, but does not seem to output it properly.

Is there a way to rectify this problem?

Note: Please no posts about not needing Javascript. Javascript is a necessary step for reasons not detailed here.

+2  A: 

They probably map the bits differently - you have a couple options. Your best option would be to find a similar encoding method that is mutual to flash and php, to save yourself some extra work. Failing that, you'll have to duplicate either flash/php's base 64 encoding in the other language.

Since you're using javascript, you should also look into phpjs and see if you can't use their base64_encode method, and then pass the string off via ajax.

Cam
I was just going to post about phpjs's version of base64_encode +rep
Nathan Adams
We aren't actually doing any encoding/decoding in Javascript. This is because the JPG byte array that AS3 returns to JS seems to lack its methods, so we are encoding it in AS3 before it gets sent to JS.
Gus
@Gus: I still don't see why you couldn't simply pass it to javascript. What do you mean by the byte array 'lacking its methods'?
Cam
A: 

Not sure about what might be causing trouble. I've passed base64 encoded data to php from AS3 a lot of times and haven't run into problems.

I generally use Hurlant's Base64::encodeByteArray() in the AS3 side, though.

Edit

Something like this might help debugging this problem:

        var buffer:ByteArray = new ByteArray();
        for(var i:int = 0; i < 256; i++) {
            buffer[i] = i;
        }

        var str:String = Base64.encodeByteArray(buffer); // this is com.hurlant.util.Base64
        // post your data to your php (directly or through JS)

Php test code:

$str = $_REQUEST['str'];

$decoded = base64_decode($str);
file_put_contents("test.txt",$decoded);

In the AS side, you are sending a string that encodes every possible byte value. The php will decode it and write it to a file. Now, if you open the generated file with an hex editor, you should see all the numbers from 0 to 255 (or 0x00 ... 0xff). If this is not the case, this might give some hints on what's causing the problems.

PS: I'd post from AS to PHP directly just to simplify troubleshooting. Maybe JS is messing with your data somehow (not likely since base64 should be safe); but just to discard a possible source of the problem.

Juan Pablo Califano
A: 

Base64 encoding is part of the MIME rfc (http://www.faqs.org/rfcs/rfc2045.html), so the php and the flash version shouldn't behave differently.

When having problems with php functions you should always read some of the comments in the PHP manual (http://ch2.php.net/base64_decode). I found two people having problems (and solutions) for similar problems by just reading the top comments.

Start with the first comment (http://ch2.php.net/manual/en/function.base64-decode.php#92980), which is about base64_decode() issues when the string to be decoded is large (>5k). The proposed solution is to use something like this:

<?php
$decoded = "";
for ($i=0; $i < ceil(strlen($encoded)/256); $i++)
   $decoded = $decoded . base64_decode(substr($encoded,$i*256,256));
?> 

I'm sorry, I'm not aware of a better alternative.

svens
A: 

I passed base64 encoded string from php to flash and back many times using this class on flash side com.dynamicflash.util.Base64 (You may download SWC and link it to to your project)

on php side: standart library functions base64_encode and base64_decode

All data process without errors

terbooter