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.
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.
-
// url vars
-
var imageFolder:String = "images/";
-
var uploadScript:String = "fileUpload.php";
August 12th, 2005 on 1:57 PM
Yes but can you upload objects generated from within Flash itself, for instance upload a printToBitmap to the server?
August 12th, 2005 on 2:11 PM
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.
August 12th, 2005 on 2:41 PM
I see, so it’s basically just replicating what can already be done via the browser. Not so exciting, but thanks
August 12th, 2005 on 2:53 PM
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.
August 12th, 2005 on 3:39 PM
Cool! But you still need scripting on the server side right?
August 12th, 2005 on 4:26 PM
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.
August 19th, 2005 on 10:02 AM
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.
August 19th, 2005 on 11:25 AM
Once I get the source cleaned up, I’ll post it. It’s pretty messy right now.
October 11th, 2005 on 3:52 AM
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!
October 11th, 2005 on 2:37 PM
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
October 11th, 2005 on 2:56 PM
You can also leave the fileTypes array empty to allow any type of file to be selected and then uploaded. The
type==1stuff in the PHP has to do with theexif_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.
October 11th, 2005 on 3:12 PM
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.
October 11th, 2005 on 3:12 PM
No problem.
October 24th, 2005 on 3:55 PM
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
October 24th, 2005 on 4:12 PM
Sorry about that. Looks like I accidentally deleted them when I was doing some maintenence. The source files are back online.
October 28th, 2005 on 8:25 PM
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?
October 29th, 2005 on 3:59 PM
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?
November 3rd, 2005 on 4:00 AM
How can I manage to display image uploaded in a movie different that the one where the script and button is? Thanks
November 3rd, 2005 on 7:38 AM
Nice app,
is it possible to upload the image, change its height/width, colours etc and then save it to the server?
thanks
November 11th, 2005 on 6:55 PM
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
November 11th, 2005 on 8:17 PM
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
December 10th, 2005 on 7:15 AM
hi Josh,
is this PHP script for PHP 4 or do i need PHP 5?
regards,
Elvis.
December 10th, 2005 on 7:52 AM
The PHP should work for either version 4 or 5. For sure on 4. (That’s what my server is using.)
December 12th, 2005 on 8:33 PM
I test the sample in xampp,I found it no work?why.
December 15th, 2005 on 11:23 PM
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
December 16th, 2005 on 7:33 AM
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
December 16th, 2005 on 9:00 AM
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.
December 18th, 2005 on 7:57 PM
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?
December 30th, 2005 on 12:12 PM
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.
January 5th, 2006 on 10:25 AM
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.
January 16th, 2006 on 3:54 AM
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.
January 20th, 2006 on 11:03 AM
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
February 1st, 2006 on 8:26 AM
Does anybody no how to cure the problem of error 406 on flash upload in flash 8??
Cheers in advance
February 2nd, 2006 on 7:38 AM
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
February 21st, 2006 on 1:30 AM
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.
February 21st, 2006 on 2:20 PM
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
March 22nd, 2006 on 10:25 AM
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?
March 27th, 2006 on 4:11 AM
maybe your php was compiled without the GD library… check phpinfo();
April 19th, 2006 on 6:45 AM
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.
April 23rd, 2006 on 3:22 AM
Thanks so much for the .htaccess solution dudes!
-Evan
June 12th, 2006 on 9:51 AM
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/”
June 12th, 2006 on 10:04 PM
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
June 12th, 2006 on 10:06 PM
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
September 7th, 2006 on 2:37 PM
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
November 24th, 2006 on 8:41 AM
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.
January 19th, 2007 on 1:25 AM
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
January 24th, 2007 on 12:48 AM
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.
January 24th, 2007 on 9:52 AM
@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.
January 31st, 2007 on 3:08 PM
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?
February 1st, 2007 on 7:32 AM
@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.
March 22nd, 2007 on 2:24 AM
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.
April 27th, 2007 on 6:13 PM
THis would be good if I knew the PHP code to be used. Rather useless without. Any idea at all
September 20th, 2007 on 8:40 AM
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?
November 4th, 2007 on 3:10 PM
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?
November 5th, 2007 on 6:55 PM
For some reason i can’t see the preview in flash of the uploaded image?? help please?
December 12th, 2007 on 1:03 PM
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?
August 13th, 2008 on 3:49 AM
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?