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.

[as]//– 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;
request.data = bytes;
loader.load( request );[/as]

And of course you need a PHP file like this:

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

Related Posts

17 Responses to “Save ByteArray to file with PHP”


  1. 1 anil

    how we will go abt same when we need to implement it using asp or asp.net
    in this snippet i am confussed abt variables like
    bytes in request.data = bytes;
    where is data within bytes variable to write the file

    Thanks
    anil http://www.ade-technologies.com

  2. 2 joa

    You have to fill the bytes variable with your file.
    And I have no clue about ASP(.NET).

  3. 3 Michiel

    thanx, after 4 hours of reading AMFPHP manuals on Remote Objects in Flex 2, this was just the simple bit of script I needed. thanks a lot.

  4. 4 Neil

    How would I go about retrieveing said ByteArray data from the server using amfphp and having Flash rebuild the BitmapData that was stored as a ByteArray? This would be for Flash CS3 using AS3. Is it even possible?

  5. 5 joa

    AMFPHP can handle ByteArrays. So you can retrieve the ByteArray and put it into a Loader via loadBytes(). When the Loader is complete you can draw the Loader on a BitmapData. But honestly this does not make much sense. Why not load the file directly without AMFPHP?

  6. 6 Neil

    Well the file is being stored in the MySQL db right now. We had a semi working version earlier today. It worked only if the data wasn’t saved to the db. We tried something to see if it was indeed the data storage or if it was that the data wasn’t getting there properly. So we had php receive the data and then create a new bytearray from that data and send it back to flash and in that case we got the images drawing back. Once things got to the db tho it went sideways. We ended up going the Base64 route for today. We’re going to look at another way in the morning. The base64 added so much time and size to the files. About 33% larger and up to 4x slower for upload/download/processing. The reason we don’t just load the file with out the amfphp is that there is no file. All of the image data is stored as blobs in the db. I suppose we could rebuild the images on the server and then pass back urls as well. Maybe we’ll loook at that in the morning as well. If you’d like to talk about this more Joa I’d be more than happy to.

  7. 7 smack

    it’s not really working for me.
    all I get is 0 sized files :( as if HTTP_RAW_POST_DATA isn’t there.
    can you post a working example?
    I am trying to save a test BitmapData object on the server, as a JPEG, and cannot use AMFPHP (the client refuses) and your solution seems to be the best, but it’s not working for me

    Thanks

  8. 8 Roman

    That’s good. But is there any way to post it to $_FILES ? like a simple file upload because I want some more fields to be posted.

    Thanks!

  9. 9 joa
  10. 10 Michael

    This info is very good. Thanks for your works :)

  11. 11 Jonathan

    Thanks for the help. I had everything except the PHP working. It seems so easy now. Big help.

  12. 12 bubbleboy

    thanks! very useful!

  13. 13 Gigs

    Here’s an ASP.net solution for saving/outputing the file.

    I also wanted to save the file (in this case a png) to the server so I’ve included this code too.

    Here’s how I solved the problem:

    Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles MyBase.Load

    ‘ Get the image file byte data from the HTTP request

    Dim fileData As Object
    fileData = Request.BinaryRead(Request.TotalBytes)

    ‘ Set the filename
    Dim strFileName As String = “yourFilename.png”

    ‘ Map the path to the relevant save folder
    Dim strPath As String = Server.MapPath(”files/”)
    strPath = strPath & strFileName

    ‘ write the file to disk
    Dim oFile As FileStream
    oFile = File.Create(strPath)
    oFile.Write(fileData, 0, Request.TotalBytes)
    oFile.Close()

    ‘ push the file to the user
    Response.AddHeader(”Content-Disposition”, “attachment; filename=” & strFileName)
    Response.AddHeader(”Content-Type”, “image/png”)
    Response.ContentType = “application/octet-stream”
    Response.BinaryWrite(fileData)

    End Sub

    You’ll need to import System.IO in your ASPX file and the IUSR account will need write permission to the folder where you want to save the image.

    Hope this helps the non-PHPers!

  14. 14 Anthony

    I was able to succesfully get a graphics image up to the server using php however i would like to have a dynamic filename. How would I go about doing that?

  1. 1 Jooce Blog » Real file upload with AS3
  2. 2 Just Flashing - Today’s Top Blog Posts on Flash - Powered by SocialRank
  3. 3 DClick Blog

Leave a Reply






Close
E-mail It