joshbuhler.com

Uploading Files in Flash 8

by Josh on Aug.12, 2005, under Flash

One of the big new features in Flash 8 is the ability to upload files. While this demo isn't the most exciting, or as flashy as others that have been posted, it still serves it's purpose.

I built it more to see what I can do with the uploading abilities, and just threw in some basic controls like scaling and rotation so that I could have something to do with the image once it's uploaded.

Link: http://www.joshbuhler.com/flash8/upload/

I have temporarily removed the upload example, due to some attempts to upload malicious files. Once I have time to adjust the example to handle this, I'll repost it. However, the source files you can install on your own server will still be available for download.

(I assume it's a given, but you will need Flash Player 8 for this to work.)

UPDATE: Source files are now available for download.

Source Files

One note: depending on where you put these files, you may need to adjust the url's used by the swf. You'll find them in main.as beginning on line 13.

  1. // url vars
  2. var imageFolder:String = "images/";
  3. var uploadScript:String = "fileUpload.php";


57 comments for this entry:
  1. sal

    Yes but can you upload objects generated from within Flash itself, for instance upload a printToBitmap to the server?

  2. Josh

    If you used printToBitmap, that would still only send the image to the user’s printer. However, if they were to print that as a pdf, for example, and you needed that on your server, you could have the user upload the pdf back to the server. (Crappy solution, I know.)

    This is only for uploading external files to the server. What you do with the file once it’s uploaded depends on your needs.

    In this case, I’m only uploading an image to display it and modify it. Or, if you were to build something like a Flash-based email client, you could use this to add attachments to the message you’re about to send.

  3. sal

    I see, so it’s basically just replicating what can already be done via the browser. Not so exciting, but thanks :)

  4. Josh

    Right. It can already be done in the browser using something like php, but the nice thing here is that while it seems pretty basic, it’ll make things a lot easier for a lot of Flash apps.

    Previously, you’d need to do some interesting workarounds to upload something to your Flash-app, and now you don’t need to.

  5. sal

    Cool! But you still need scripting on the server side right?

  6. Josh

    Yup. Just like an html page, you’d need to have some sort of server-side script to handle the actual upload of the file to the server. This example uses a php script to take care of the upload, but really it could be JSP, ColdFusion, etc. Flash 8 just now gives you the ability to open a “Browse…” dialog, and then pass the file to the server.

  7. Rob

    Hey, would it be possible to have a copy of the FLA for this sample. Also, would like to have the PHP script as well. If possible. I know that you don’t have to if you don’t want to.
    Thanks.

  8. Josh

    Once I get the source cleaned up, I’ll post it. It’s pretty messy right now.

  9. tobijas

    I’ve used yamzBrowser project to make CMS for Flash MX (with Flash control panel). Yamaz was hack, simulating Flash button with JavaScript button behind. It was great with IE, but other browsers had problems. Now, with Flash 8 it’s so easy :) Thanx for posting your script!

  10. Jeff Dernbach

    I really like your example. Is there a way to upload different file types then jpg. gif. png?

    I have tried several different methods like adding different file type extensions to the array in the main.as and trying to modify the array in the php script i.e. adding type=4, type = 5 and so on. Did I miss a list that correlates the filetype to type from Flash to the php script.

    I noticed the example on live docs (macromedia) had the same restriction for jpg, gif, png only.

    Thank you

    Jeff

  11. Josh

    You can also leave the fileTypes array empty to allow any type of file to be selected and then uploaded. The type==1 stuff in the PHP has to do with the exif_imagetype() being used. This is a built-in PHP function that gets various bits of info about an image file.

    If you’d like to upload file types other than images, you’d need to modify the PHP to not check the image type, or write a new script that can handle any file type, instead of just images. When I put together this example, I was just a little lazy, and didn’t want to write my own upload PHP script, so I just used the example one from Macromedia.

  12. Jeff Dernbach

    Thank you for helping me on this. I was going to write an asp script to work with the file upload in Flash 8, and have been scouring for examples to get around the live doc 200 k limit and 3 file type limit. You helped me a lot thanks.

  13. duranike

    um, i think the file link is down. can someone send me the file or is the file going to be put back up?

    thanks

  14. Josh

    Sorry about that. Looks like I accidentally deleted them when I was doing some maintenence. The source files are back online.

  15. pixelone

    I like you upload system. The preloader is very helpful. Something I come accross with all the upload tutes for Flash 8 that I come across - You CANNOT upload videos i.e .mov, or any video files.

    I even change the file types so that the filter includes .mov.

    I select the .mov file, but it does not show in the folder.

    Can anyone help me on this?

  16. Jester

    I have the same question and issue as well, not able to upload video files. Only image files will work. Is that all flash 8 can do?

  17. Mario

    How can I manage to display image uploaded in a movie different that the one where the script and button is? Thanks

  18. johan

    Nice app,

    is it possible to upload the image, change its height/width, colours etc and then save it to the server?

    thanks

  19. joel

    Hi,

    Is it possible to limit the size of the upload file? Like where you would like to avoid somebody uploading a 50mb file to your server. Would this be made possible with php or directly within flash?

    Thanx a lot,
    joel

  20. Josh

    You can limit the size either in PHP or in Flash.

    The best way would be to prevent it in Flash. Once you select the file you’re going to upload, use FileReference.size to check the filesize of the selected file, and if it’s too big, warn the user, or don’t allow them to upload it. That way, you don’t have to waste time uploading unless it’s the right size.

    http://livedocs.macromedia.com/flash/8/main/00002223.html

  21. elvis

    hi Josh,

    is this PHP script for PHP 4 or do i need PHP 5?

    regards,

    Elvis.

  22. Josh

    The PHP should work for either version 4 or 5. For sure on 4. (That’s what my server is using.)

  23. video

    I test the sample in xampp,I found it no work?why.

  24. Vishwanath

    Hi,
    I need to know what is the maximum size of the file that can be upload using the HTTP post , i have a problem in uploading the files across the network , however the same piece of code works well with in the network and i am able to upload a file up to 100 MB (with in Netwrok) where as when i try to uplaod a file around 10 MB across the Network it fails saying “Page Cannot be Displayed ” after some time.
    Thanks,
    Vishwa

  25. Vic

    Hi!

    Great work, I wanted to check out the *fla but i can’t open it in Flash MX professional 2004…Is there such a version for download?

    Regards,
    Vic

  26. Josh

    Vishwanath - From what I understand, there is no upload limit. However, only files up to 100MB were tested by Macromedia, and above that, you’re on your own.

    Are you uploading to the same server in each case? You may want to check your server settings to see what (if any) limits there are on uploaded files.

    Vic - There is no download for Flash MX 2004. Uploading is a Flash 8 only feature, so there’s no use in posting a source file for older versions.

  27. Rory

    When I test the movie fileUpload.fla, I get the error message:

    **Error** /Files from FreeTransform.as: Line 7: The name of this class, ‘FreeTransform’, conflicts with the name of another class that was loaded, ‘FreeTransform’.
    class FreeTransform {

    Total ActionScript Errors: 1 Reported Errors: 1

    and when I run it, it loads the file but never gets as far as loading the image. Any suggestions?

  28. Elton

    I have the same question as a previous poster, but I didn’t see if it had been answered.

    Is it possible to upload the image, change its height/width, colours etc and then save it to the server?

    Thank You.

  29. Coop

    Upload problems might be caused by folder permissions issues. Make sure the script is pointing to a directory that you’ve CHMOD’ed to be writeable.

    Great script.

  30. Luke

    Nice work. It’s great to see some good examples avaliable to the public at no charge. Keep up the good work and dont bother answering those stupid questions.
    Luke. Macromedia.

  31. david

    Tried the flash upload in flash 8 and it worked!! and then it didn’t I got a range of errors today’s error is 406.
    My server provider is at a loss and so am I can anyone shed some light on what might be the problem.
    The permissions on the folder are correct and the code is macromedias own which worked at the start.

    Any help would be great.
    Dave

  32. david

    Does anybody no how to cure the problem of error 406 on flash upload in flash 8??

    Cheers in advance

  33. Brent

    I’ll ask the same question as two other people and hope it gets answered….

    Is there a way to save the file after you have messed with the attributes such as the color and shape, pref. overwritting the file?

    Thanks,
    Brent

  34. Josh

    You may want to try building an HTML form that uploads to the PHP script to make sure it works that way.

    Unfortunately, I’m not very good at PHP, or server-side stuff, so I took the script from the example files in the Flash help docs. It’s in the “Using Data with Flash” section I believe. Or something like that.

    If you have your own PHP script that does file uploads, start with that. Make sure that you can upload from a standard HTML form to your script, then, once that’s working, try it from Flash. The script should work from either, because Flash is doing essentially the same thing as the form would when sending the files to the PHP.

  35. Mike R

    THANK YOU MONTIN!!!!! I never would’ve thought to check out mod_security with my host. This was the exact solution to my 403 problem (and other people’s 406 problem). My host upgraded php without telling me and added mod_security. All I had to do to remove this problem is create an .htaccess file in my root directory with:

    SecFilterEngine Off
    SecFilterScanPOST Off

    Thank you very much once again,
    Mike

  36. Ben

    Awesome work. I have trouble with the php file though. (I saw your disclaimer.)

    Anyone know why I would be getting this error?

    Fatal error: Call to undefined function exif_imagetype() in C:\Program Files\Apache Group\Apache2\htdocs\flash\fileUpload.php on line 16

    exif_imagetype should be available with php 5 right?

  37. serban

    maybe your php was compiled without the GD library… check phpinfo();

  38. Folkert

    Well mailing that to MM ‘as’ solution seems wrong to me. The htaccess ’solution’ is a work around and should be considered as ‘temporary’ i hope at least. Those mod_security settings are there for a reasons, and the bloking of the header from flash, is a flash related problem, they should fix the header, then the server is save and the upload will work.
    just 2 cents from Holland.

  39. Evan M-S

    Thanks so much for the .htaccess solution dudes!

    -Evan

  40. Jonathan Layne

    I am having trouble seaprating the php file and the swf, if I keep them in the same folder everything works fine but if I separate them they dont work, I obiously updated the main.as and php code. Any Ideas as to why?

    In my case I want the php code in the root:\cgi-bin folder and to write the files to a root:\cgi-bin\folder in cgi-bin, but I load all my swf from root:\swf, so in main.as I changed var imageFolder:String = “folder/”;
    var uploadScript:String = “../cgi-bin/fileUpload.php”; and in php I changed all the “./images/” to “folder/”

  41. threebrain

    this is rad! I have a problem though. Once I upload the image to the server i want to load it back into the flash realtime. I tried writing something like this:

    function getImage(){
    if(container._width

  42. threebrain

    oops my function got cut off. well basically i want it to do this:

    _root.cover.container.loadMovie(”/images/” + fileRef.name);

    how come that don’t work?

    hugs

  43. Ori

    I have to launch a site for a project I have, I did it with flash 8 and I need to give the user an option to upload a video file (from one of the tabs in the site) into mysql. Can somebody help me with the code(s) and a little bit of guide of how to do it. Important to add, I am not a developer.

    Thanks.

    Ori
    general.incmail@gmail.com

  44. rick

    Im making an upload function and Im checking the mime type in php, it works in a regular html/php page but not with the filereference.

    Anybody knows how to check if a file is a .pdf or .doc file?
    Im checking with regwex but want to add extra security.

  45. bhavin

    problems with imageuploader. somehow uploading does not work correctly.

    script shows currentUploadingItem.upload(”http://www.xxxxxxxx.com/albums/flashuploadsubmit.php”); as the upload location but instead it goes to POST /images/swf/undefined HTTP/1.1 so upload fails. /images/swf is the location of .swf file for image uploading.

    Any ides why would that be.

    Thanks

  46. dan

    I am pretty new to Flash… been playing around with Flash 8 trying to create a tool that will allow the user to browse a local image using FileReference. I want to then allow them to scale/pan the image to a certain size, etc. and then I want to upload it. Is this possible without uploading the original image to the server first and then downloading it back to a movie clip, etc? If this isn’t possible what is the reason. Thanks for any help.

  47. Josh

    @dan

    That’s not possible. While Flash 8 will allow you to browse the local file system in order to find a file to upload, Flash will not allow you to do anything with that file, other than upload it. The only way for Flash to interact with that file is after you have uploaded it to your server, then loaded it back into Flash.

  48. dan

    Thanks for the info. Another question… if I do use this process to upload and then download and then modify the image (using BitmapData?) is it possible to upload the modified image? I image I would have to use FileReference to do the upload so does it have to open a local file?

  49. Josh

    @dan

    You can save the modified image, but FileReference only works with files that the user selects to upload, or a file that you want the user to download.

    To save a modified image or any other BitmapData object, you would need to serialize that BitmapData object, and send it to the server somehow, through a series of calls to a server-side script.

    More info on that can be found here: http://www.quasimondo.com/archives/000572.php

    When I was at mediaRAIN, we did something similar to save images the user had been working on, and it worked pretty well. You definitely need to be careful though so that you don’t hang the Flash Player in the user’s browser, since you’ll have to loop through every pixel in an image.

  50. torwalker

    Is there a fix for the flash filereference problem noted above? If I add
    SecFilterEngine Off
    SecFilterScanPOST Off
    to my htaccess file, i get an HTTP 500 Internal Server Error so that’s not an option for me.
    Thanks.

  51. Ed

    THis would be good if I knew the PHP code to be used. Rather useless without. Any idea at all

  52. Garfield

    I am trying to find a solution that utilizes the browse function and loadmovie function. Basically browse your local machine for an image file, once located and selected, load that image file into your flash. Is this possible? I was able to make it work when the file selected resides in the same folder as flash file but when the flash file does not reside in the same folder as the image, then i get an error. It there a way of copying the path to that image?

  53. Andy

    Hi,

    Is it possible to upload and image, drag animated symbols on top of the image and then save the composition as either a .swf file or a .fla?

  54. iCeR

    For some reason i can’t see the preview in flash of the uploaded image?? help please?

  55. Kevin

    Thanks for this source code. Very well documented and very clean.

    I am having one issue with it and it appears to be Mac oriented only. When I click ‘browse’, all the files are grey in my browse window on the Mac. I’ve tested this on Windows XP and it works fine. So I’m fairly certain this is a Mac issue only. Unfortunately, we’re an all Mac office and I need to be able to use this here.

    I’ve tried eliminating the macfile information from the file type array, even tried making the file extentions be *.*, but I can’t seem to select a file to upload. I even went so far as to take out the filetype from the () for the file browse, but no luck.

    Any ideas on how to get around this?

  56. Paul

    Hi Great code and I use it on my site. I am having problems with MACS though. No one with a MAC can upload an image. Everything works up until the progress bar. This does not get displayed and nothing else happens. Any ideas?

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...