Flash: Force Download

June 1st, 2005 by Luis

The simplest solution to force a download of an image directly from flash. I haven't tested it in all browsers, but it works in explorer.

Sample swf: (just right click the picture to download the image):

[flash]http://www.blog.lessrain.com/wp-content/upload/test.swf,400,200[/flash]

There is nothing new here, basically the trick is a simple getURL call to a php script that forces the picture to download: AS code:

Actionscript:
  1. var myPic:String='IMG_0200.jpg';
  2. var my_cm:ContextMenu = new ContextMenu();
  3. var menuItem_cmi:ContextMenuItem = new ContextMenuItem("Download Picture", down);
  4. my_cm.customItems.push(menuItem_cmi);
  5. my_cm.hideBuiltInItems();
  6. IMG_0200.menu = my_cm;
  7.  
  8. function down() {
  9.  getURL ("swfsamples/forcedownload.php?file="+myPic);
  10. }


PHP code:

PHP:
  1. $filename = $_GET['file'];
  2.  
  3. // required for IE, otherwise Content-disposition is ignored
  4. if(ini_get('zlib.output_compression'))
  5.   ini_set('zlib.output_compression', 'Off');
  6.  
  7. header("Pragma: public"); // required
  8. header("Expires: 0");
  9. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  10. header("Cache-Control: private",false);
  11. header("Content-Type: image/jpg");
  12. header("Content-Disposition: attachment; filename=".basename($filename).";" );
  13. header("Content-Transfer-Encoding: binary");
  14. header("Content-Length: ".filesize($filename));
  15. readfile("$filename");
  16. exit();
  17. ?>


17 Responses to “Flash: Force Download”

  1. LuvCube Says:

    Does it violate any of the terms of viewing a website?

  2. Luis Says:

    What do you mean with violate any of terms of viewing a website?

  3. Darren Says:

    nice work luis :)

  4. Playfool » Download picture from flash Says:

    […] ome code to enable you to right click and down load a picture (jpg etc) from a swf file seeForce Picture Download for more information Permalink Leave a reply […]

  5. Bruno Says:

    works in firefox as well ;) well done Luis

    hey mate, I still owe you a beer. Just tell me the place and the date :) salut

  6. Thomas Says:

    if you leave the script this way somebody could easily download sensitive information from the server like php sourcefiles with passwords etc.!

    so you should always add some limitations to the script. in this case i just added the line:

    if ($filename != “IMG_0200.jpg”) exit();

    you can limit it to filetypes too, but directory access limits should really always be there…

  7. Thomas Says:

    using the following method you can compare the directory-name relative to the base-url of the website:
    $baseurl = strreplace($DOCUMENT_ROOT, “”, dirname($filename));

    in our case would return “/swfsamples”

    so you could say:
    $fileextension = strtolower(substr(strrchr($filename,”.”),1));
    if ($base
    url != “/swfsamples” || $file_extension!=”jpg”) exit();

    a combined directory/mimetype check!

  8. Luis Says:

    Actually, there are some interesting comments about the use of the header function in php.net.

    http://uk2.php.net/header

    Bruno, whenever you want, we can have that beer ;)

  9. lessrain blog » Blog Archive » Flash: Force Download… No more dirty tricks in Flash 8 Says:

    […] ore dirty tricks in Flash 8 September 16th, 2005 by Luis Following up our post Force Download where we discussed a few tricks to download a picture directly from flash using getURL and a s […]

  10. harish Says:

    I am forcing a download of image using this script.
    In flash i have just used : geturl()
    and passing this to php.

    Issue: now this works fine with IE, but in Firefox it is downloading the pic but only after removing its extension. and thus one has to add the extension manually for file to open

    • Harish
  11. Mar10us Says:

    @ Harish

    Did you leave out the spaces? Firefox got a problem with spaces in a filename…

  12. Luis Says:

    mikaël, try this:

    import flash.net.FileReference;
    var myMP3= “http://www.yourdomain.com/mp3/mymp3.mp3″;
    var fileRef:FileReference = new FileReference();

    function bringmetheMP3(){
    fileRef.download(myMP3)
    }

  13. James Says:

    So the code works great on your example but where is it applied? on the first frame of the scene or on the picture or button that is clickable?

  14. akakjb Says:

    I know that your demo works but I’m not sure how to apply the code to a swf file. where does the php code go and where does the actionscript go. I am trying to make it possible to download mp3’s.

  15. Jim Says:

    Great bit of coding.
    Works well in firefox and opera, however i cannot get the content-disposition code at the top of the php to enable the filename to be carried across in internet explorer. It just defaults to save a file ‘forcedownload.jpg’.
    Any ideas on how to sort this?
    Cheers Luis

  16. Lindsay Says:

    excellent demo, is there anyway to apply it to a button/link in flash that forces a pdf to download?

  17. iamnotbored.com » Blog Archive » flash actionscript force download Says:

    […] http://www.blog.lessrain.com/flash-force-download/ […]

Leave a Reply