I'm trying to send encoded data for a bitmap image to a server and back to the( or a separate) client. There's probably an easier way to do this with sockets or something, but I'm a very casual and still a pretty new programmer and I had trouble understanding other methods. The encoding and decoding works fine and I can save and open up the encoded png, but somewhere on the php/mysql side the encoded data loses most of it's code.
Actionscript code for sending the encoded data to the php.
var req:URLRequest = new URLRequest("http://host.net//img.php");
req.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader;
var phpvar = new URLVariables();
var shape:MovieClip = new MovieClip();
shape.graphics.beginFill(0xFF0000);
shape.graphics.drawRect(0, 0, 100, 100);
addChild(shape);
var bmp:BitmapData = new BitmapData(500, 350);
bmp.draw(shape);
var byte:ByteArray = new ByteArray();
bmp.encode(new Rectangle(0,0, 100, 100), new flash.display.PNGEncoderOptions(false), byte);
//save on database
phpvar.bytez = byte;
req.data = phpvar;
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(req);
trace(req.data.bytez);
The traced output is:
PNG
->
IHDRddÿIDATxÚíÐ1 µçYÁÏ"Ð)®F,Y²dɒ¥@,Y²dÉR
K,Y²d)%K,Y²Ȓ%K,Y dɒ%K,²dɒ%KY²dɒ%K,Y²dɒ¥@,Y²dÉR
K,Y²d)%K,Y²Ȓ%K,Y dɒ%K,²dɒ%KY²dɒ%K,Yß^Çjú··IEND®B`
Actionscript code for recieving data:
var req = new URLRequest("http://host.net//imgsend.php");
var loader = new URLLoader(req);
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(req);
function onComplete(e:Event) {
var loaderer:Loader = new Loader();
var byte:ByteArray = new ByteArray()
byte = e.target.data;
trace(byte);
}
Traced output is:
PNG
->
For some reason it knows the encoded data is for a PNG, but all there is is a header. I think the problem is in my php/mysql code, but I'm not completely sure here. I'm using a free webhosting service with myphpadmin for convenient sake, the Images table is just a BLOB column and the php files are uploaded on the host server.
PHP code for reading and inserting into mysql (img.php)
<?php
mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database) or die(mysql_error());
$pic = $_POST['bytez'];
$sql = "INSERT INTO Images (img) VALUES ('$pic')";
mysql_query($sql);
?>
PHP code for taking the data form the server and sending it to Actionscript (imgsend.php)
<?php
mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database) or die(mysql_error());
$sql = "SELECT * FROM Images";
$records = mysql_query($sql);
while($byte=mysql_fetch_assoc($records)){
echo $byte['img'];
}
?>
I know the php code is sloppy at best, and I'll deal with processing what gets fetched by the query when the time comes. Any help would be great, even if you just help me clean up the sloppy code lol.
Answers
I didn't necessarily find an answer to this exact method, but I found an alternate solution, ie writing the file directly into the directory. From what I've read this is much more efficient anyways. The code is basically the same only I added URLcontentType and extended the URL for the URL request address for the file's location.
var req:URLRequest = new URLRequest("http://host.net//img.php?filename=something.png");
req.method = URLRequestMethod.POST;
req.contentType = "application/octet-stream";
var loader:URLLoader = new URLLoader;
var shape:MovieClip = new MovieClip();
shape.graphics.beginFill(0xFF0000);
shape.graphics.drawRect(0, 0, 100, 100);
addChild(shape);
var bmp:BitmapData = new BitmapData(500, 350);
bmp.draw(shape);
var byte:ByteArray = new ByteArray();
bmp.encode(new Rectangle(0,0, 100, 100), new flash.display.PNGEncoderOptions(false), byte);
//save on database
req.data = byte;
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(req);
The PHP code is for img.php is:
<?php
$fileName = $_GET['filename'];
$fp = fopen( $fileName, 'wb' );
fwrite( $fp, $GLOBALS['HTTP_RAW_POST_DATA'] );
fclose( $fp );
?>
Loading the file is simple, not going to bother posting that. I'll deal with unique file names on my own, baby steps. Thanks for helping me talk this out some!
コメント