通过AS3到PHP发送的ByteArray [英] Sending ByteArray through as3 to PHP

查看:241
本文介绍了通过AS3到PHP发送的ByteArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BLOB字段(PIC)是谈到了为0字节试图通过AS3到PHP发送的ByteArray的时候,所以我想在PHP脚本或HTTP_RAW_POST_DATA是行不通的。

我觉得闪存部分工作,我设置了跟踪()来看看如果BitmapData通过来了,现在看来,这是,所以我假设它我的PHP的一面。我会在希望有人张贴的code这两个部分在这里都可以给我。谢谢你。

AS3

 私有函数的出口():无效
    {
        VAR BMD:的BitmapData =新的BitmapData(600,290);
        bmd.draw(板);
        变种BA:的ByteArray = PNGEn coder.en code(BMD);
        跟踪(BA);
        VAR _request:的URLRequest =新的URLRequest(http://site.com/readimage.php);
        VAR装载机:的URLLoader =新的URLLoader();
        _request.contentType =应用程序/八位字节流;
        _request.method = URLRequestMethod.POST;
        _request.data = BA;
        loader.load(_request);
    }
 

PHP

 < PHP
$用户名=形象;
$密码=密码;
$主机=localhost的;
$数据库=形象;

$链接=的mysql_connect($主机,$的用户名,密码$)
如果(!$链接){
死亡(无法连接:mysql_error());
}
mysql_select_db($数据库);

$查询=INSERT INTO主(图)VALUES('$ GLOBALS [HTTP_RAW_POST_DATA]。')或死亡(mysql_error());
$结果=请求mysql_query($查询,$链接);
?>
 

解决方案

  $ BLOB =的file_get_contents('php的://输入');
 

这应该为你工作。这种访问 PHP的原料输入流。它更可能在某些情况下工作,显然:

  

PHP://输入,您可以请求主体读取原始数据。在POST请求来说,它preferrable到 $ HTTP_RAW_POST_DATA ,因为它不依赖于特殊的php.ini设置。此外,对于这些情况下, $ HTTP_RAW_POST_DATA 不填充默认情况下,它是一种潜在更少的内存密集型的替代激活 always_populate_raw_post_data

你还需要确保你正确地放置时,它在数据库中逃脱这个数据:

  $查询=INSERT INTO主(图)VALUES('mysql_real_escape_string($ BLOB)。');
 

(它也可能是 $ HTTP_RAW_POST_DATA 的魔法,只有当你引用它,而不是直接通过 $ GLOBALS作品阵列。)

The BLOB field (pic) is turning out as 0 Bytes when trying to send ByteArray through as3 to PHP, so i assume the PHP script or the HTTP_RAW_POST_DATA isn't working.

I think the Flash part is working, I have set a trace() to see if the bitmapdata is coming through and it seems it is, so I'm assuming its my php side. I'll post both parts of the code in hope someone here can fix it for me. Thanks.

AS3

    private function export():void
    {
        var bmd:BitmapData = new BitmapData(600, 290);
        bmd.draw(board);
        var ba:ByteArray = PNGEncoder.encode(bmd);
        trace(ba);
        var _request:URLRequest = new URLRequest ("http://site.com/readimage.php");
        var loader: URLLoader = new URLLoader();
        _request.contentType = "application/octet-stream";
        _request.method = URLRequestMethod.POST;
        _request.data = ba;
        loader.load(_request);
    }

PHP

    <?php
$username = "images";
$password = "password";
$host = "localhost";
$database = "images";

$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db ($database);

$query ="INSERT INTO main (pic) VALUES ('".$GLOBALS["HTTP_RAW_POST_DATA"]."')" or die(mysql_error());
$results = mysql_query($query, $link);
?>

解决方案

$blob = file_get_contents('php://input');

This should work for you. This accesses PHP's raw input stream. It's more likely to work in some cases, apparently:

php://input allows you to read raw data from the request body. In case of POST requests, it preferrable to $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data.

You'll also want to ensure that you properly escape this data when placing it in the database:

$query = "INSERT INTO main (pic) VALUES ('" . mysql_real_escape_string($blob) . "')";

(It is also possible that $HTTP_RAW_POST_DATA's magic only works when you reference it directly instead of through the $GLOBALS array.)

这篇关于通过AS3到PHP发送的ByteArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆