Save ByteArray to file with PHP

Just a short snippet of code to save a ByteArray into any file. I post this here because there is no good example on the web.

//-- your byte array you want to save
var bytes: ByteArray = new ByteArray();

//-- set up correct url request using post in binary mode
var request:URLRequest = new URLRequest ( 'http://pathto/save.php' );
var loader: URLLoader = new URLLoader();
request.contentType = 'application/octet-stream';
request.method = URLRequestMethod.POST; = bytes;
loader.load( request );

And of course you need a PHP file like this:

$fp = fopen( 'file.txt', 'wb' );
fwrite( $fp, $GLOBALS[ 'HTTP_RAW_POST_DATA' ] );
fclose( $fp );


  1. Matt
    Posted Mar 8, 2012 at 7:29 pm | Permalink

    Im attempting to use your code – but I must not understand. I am getting a byte array back from a WCF service, and getting the array from a POST. Code is below:

    $idPDF, “username” => $userPDF, “password” => $passPDF);
    $data_string = json_encode($data);
    $ch = curl_init(‘http://*****.com/***/DealPdf’);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, 1 );
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    ‘Content-Type: application/json’)
    $result = curl_exec($ch);

    If I understand correctly, the “$result” is the byte array. But I dont know how to get it out of the byte array and save it as a PDF. Using your example, It isnt working. What am I missing?

  2. joana
    Posted Mar 17, 2012 at 10:17 am | Permalink

    can you please send me the .fla and th php. i badly need it..
    i want my image to be save directly on the database and not in the desktop.. how will i do this.. Waiting for your reply. Thanks

  3. Posted Jun 7, 2012 at 8:11 am | Permalink

    oohhh yeah! Thanks, 3 days but you save me.

  4. Posted Jul 9, 2012 at 10:26 am | Permalink

    If I read from file system. and overwrite again. This solution does still work?

  5. Maria Lopez
    Posted Nov 12, 2012 at 2:27 pm | Permalink

    Works like a charm, thanks!