Sending ByteArray through as3 to 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(6开发者_开发百科00, 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 activatingalways_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.)
Try breaking apart your whole process - if it's not working, start stripping away things before you get as far the sql insert...
First off, open up firebug or chrome/safari console and log your data being passed to your php page - then perhaps just start seeing what's being passed:
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
If you have the console open, it should log the echo's to that.
精彩评论