Flash 到 PHP base64 编码/解码

发布于 2024-09-04 01:28:57 字数 397 浏览 5 评论 0原文

我正在使用 AS3 对 JPG 进行 base64 编码并将其传递给 Javascript。我正在尝试使用 AJAX 将 base64 编码的字符串传递给 PHP 并让 PHP 对其进行解码。 (我知道我可以将其发送回 Flash,Flash 可以对其进行解码并将其发送到 PHP,但我正在尝试消除解码端对 Flash 的需求)。

看来AS3的encodeToBase64String()函数和PHP的base64_decode()函数不使用相同的算法,因为PHP将其评估为base64编码对象,但似乎并不才能正确输出。

有办法解决这个问题吗?

注意:请勿发布有关不需要 Javascript 的帖子。 Javascript 是必要的步骤,原因在此未详细说明。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

阳光①夏 2024-09-11 01:28:57

他们可能以不同的方式映射这些位 - 你有几个选择。您最好的选择是找到一种与 flash 和 php 通用的类似编码方法,以节省一些额外的工作。如果做不到这一点,您将不得不用另一种语言复制 flash/php 的 Base 64 编码。

由于您使用的是 javascript,因此您还应该查看 phpjs 并查看是否无法使用其 base64_encode 方法,然后通过 ajax 传递字符串。

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.

非要怀念 2024-09-11 01:28:57

不确定什么可能会造成麻烦。我已经多次将base64编码的数据从AS3传递到php并且没有遇到问题。

我通常使用 Hurlant 的 不过,AS3 端的 Base64::encodeByteArray() 。

编辑

这样的东西可能有助于调试这个问题:

        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测试代码:

$str = $_REQUEST['str'];

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

在AS端,您正在发送一个对每个可能的字节值进行编码的字符串。 php 将对其进行解码并将其写入文件。现在,如果您使用十六进制编辑器打开生成的文件,您应该会看到从 0 到 255(或 0x00 ... 0xff)的所有数字。如果情况并非如此,这可能会提示导致问题的原因。

PS:我会直接从 AS 发布到 PHP 只是为了简化故障排除。也许 JS 以某种方式弄乱了你的数据(不太可能,因为 base64 应该是安全的);但只是为了排除问题的可能根源。

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.

诗酒趁年少 2024-09-11 01:28:57

Base64 编码是 MIME rfc 的一部分 (http://www.faqs.org/rfcs/rfc2045 .html),因此 php 和 flash 版本的行为不应有所不同。

当 php 函数出现问题时,您应该始终阅读 PHP 手册中的一些注释 (http://ch2.php .net/base64_decode)。通过阅读热门评论,我发现两个人有类似问题的问题(和解决方案)。

从第一条评论开始(http://ch2.php。 net/manual/en/function.base64-decode.php#92980),这是关于当要解码的字符串很大(> 5k)时的 base64_decode() 问题。建议的解决方案是使用这样的东西:

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

对不起,我不知道有更好的选择。

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.

方觉久 2024-09-11 01:28:57

我多次使用将base64编码的字符串从php传递到flash并返回
这个类在 flash 端
com.dynamicflash.util.Base64
(您可以下载 SWC 并将其链接到您的项目)

在 php 端:
标准库函数base64_encode和base64_decode

所有数据处理均无错误

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文