Unable to save image to database! HELP please T_T

Hey guys, i am having problem uploading a byte array to my database. I am new here… Please help me :pirate:

thanks in advance!
this is my codes.

var ba:ByteArray = PNGEncoder.encode(bmd);
					
					
					var _request:URLRequest = new URLRequest                 ("http://localhost/image/readimage.php");
					var loader: URLLoader = new URLLoader();
					_request.contentType = "application/octet-stream";
					_request.method = URLRequestMethod.POST;
					_request.data = ba;
					loader.load( _request );

and this is my php code

<?php
ini_set('display_errors', 1);
ini_set('log_errors', 0);
ini_set('error_reporting', E_ALL);


$username = "root";
$password = "mysql";
$host = "localhost";
$database = "user";

// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

// Select your database
mysql_select_db ($database);



$fp = fopen("file.txt", "wb");
if(fwrite($fp,$GLOBALS["HTTP_RAW_POST_DATA"])) {
	echo "writing=Ok";
	
	
	
	$query ="INSERT INTO kc (pic) VALUES ('HTTP_RAW_POST_DATA')" or die(mysql_error());
	
 	$results = mysql_query($query, $link);
 }
else {echo "writing=Error";}

fclose($fp);


?>

      

i am able to upload some data, but its not the image of data. is more like some junk data.