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]

36 Responses to “Save ByteArray to file with PHP”


  • 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

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

  • 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.

  • 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?

  • 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?

  • 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.

  • 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

  • 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!

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

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

  • thanks! very useful!

  • 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!

  • 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?

  • Oh man, perfect :) Every example I could find was some giant complicated thing — I knew there had to be a simpler way of doing it. This is exactly what I needed, thank you. :)

  • it really helped.
    i used this method for saving pictures from flash.
    in this page.
    http://www.ezzal.com/sketch.html

    good luck..

    - Dharshan

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

    Please help me . i have triad ur example it receive data but how to reuse that data from server to rebuild the BitmapData

  • Hi!

    I trying to find a flash based draw app solution for an
    intranet with save to server capability with no luck.

    Anyone can help point to such an app. Requirements:

    2) basic drawing, colors and text
    3) ability to load from server
    4) ability to save to server ( jpg, gif, or whatever )

  • Why not just use a Loader in that case? Or is your image uncompresed and not a PNG/JPEG? This would be a really bad idea.

  • thanx, But plz can u share the code with me how to implement this. i have to save drawing api to server and then reuse it from server to flash

  • How to retrieve byteArray from server to redraw it on FLASH canvass in AS3.0 and PhP??

  • Loader.load() if it is a PNG/JPEG.

  • can u tell how to retrive byte array from server to redarw image on flash , i dont want to load jpeg directly i want to redraw that image by bytearray

  • How do you echo a “success” from PHP, then have the Flash trigger an onComplete handler so we can have the flash post to the PHP file, then listen for a success.

  • wow finally a simple PHP example for us flash/flex heads.

    THANKS

    -sean http://www.onegiantemedia.com

  • Hi, Thanks for the example. Does anyone have any idea how to do this with JSP. I have never looked as JSP before today.

    Any help much appreciated.

  • how would i use this with the jpeg encoder to save the resulting saved image to a folder?

  • you cant imagine how this has helped me, thank you!!!!

  • Wow! Thank you so much for this code. i have been playing around for hours and gotten nowhere, this code was exactly what I needed.

    http://www.joshuacrigger.com/

  • Thanks a lot bro, its really great small post i am now working on large files upload in chunks and that all is based on your small piece of code. thanks you really help!

    i will publish my code when its done.

  • I’ve a question about the security of this method.

    There is no security, right?

    Everybody can send a request to the requestUrl and insert chars in the file specified or am I wrong?

  • Why its creating 0 byte file?

  • Great snippet thanks!
    I’m looking for a way to pass the file name of the newly created image back to Flash so I can write the url to a database.

    I’ve been trying to get the value from contentLoaderInfo but I don’t think that property is available…

  • Thanks a lot. I lost hours because I was not setting
    request.contentType = ‘application/octet-stream’;

  • Greate example!
    Grazie ;)

Leave a Reply