drvxnivoqf17568697 2011-03-22 14:33
浏览 75
已采纳

通过as3将ByteArray发送到PHP

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);
?>
  • 写回答

2条回答 默认 最新

  • doujiyong7604 2011-03-22 15:20
    关注
    $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.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献