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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题