![]() |
|||||||
Hybrid Hacking provides tutorials on how to integrate with some of today's hottest web services.
|
Uploading Files from Flex using PHP
Tue, 07/17/2007 - 19:25
The process is very similar to how this is typically done in HTML. In HTML, you create a form, add a file input field and submit that form to a server side script using the POST method. The first thing to do is create a PHP script on your web server that will receive the file. The following code is a simple example that demonstrates the bare basics: <?php $tempFile = $_FILES['Filedata']['tmp_name']; $fileName = $_FILES['Filedata']['name']; $fileSize = $_FILES['Filedata']['size']; move_uploaded_file($tempFile, "./" . $fileName); ?> This script moves the temporary file object uploaded through POST data to a concrete location. In this case the file is moved to the same folder the script is in. Note: There are potential security concerns to be aware of with this example PHP code. See the PHP manual for guidelines on safely uploading files using PHP. The next step is to create the Flex application. We use ActionScript to build a URLRequest, attach the file object, and send it out. Here is the MXML code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();">
<mx:Script>
<![CDATA[
private var urlRequest:URLRequest;
private var fileReferenceList:FileReferenceList;
private var serverSideScript:String = "http://localhost/uploadFile.php";
private function init():void {
urlRequest = new URLRequest(serverSideScript);
fileReferenceList = new FileReferenceList();
fileReferenceList.addEventListener(Event.SELECT, fileSelectedHandler);
}
private function uploadFile():void {
fileReferenceList.browse();
}
private function fileSelectedHandler(event:Event):void {
var fileReference:FileReference;
var fileReferenceList:FileReferenceList = FileReferenceList(event.target);
var fileList:Array = fileReferenceList.fileList;
// get the first file that the user chose
fileReference = FileReference(fileList[0]);
// upload the file to the server side script
fileReference.addEventListener(Event.COMPLETE, uploadCompleteHandler);
fileReference.upload(urlRequest);
// update the status text
statusText.text = "Uploading...";
}
private function uploadCompleteHandler(event:Event):void {
statusText.text = "File Uploaded: " + event.target.name;
}
]]>
</mx:Script>
<mx:Label text="Upload File From Flex to PHP" fontWeight="bold"/>
<mx:Label text="Choose a file..." id="statusText"/>
<mx:Button click="uploadFile();" label="Upload File"/>
</mx:Application>
To get this working on your own, upload the PHP script to your web server, create a new Flex application with the above code, and then change the serverSideScript variable to point to the proper location. When everything works properly the file will be uploaded and the uploaded file name will be displayed in the Flex app as shown below.
Comments
Mon, 12/24/2007 - 02:16 — Anonymous (not verified)
Great tutorial,
Thu, 12/27/2007 - 18:54 — Chris Charlton (not verified)
Check this post out, shows how to return data from the upload. http://www.dgrigg.com/post.cfm/08/02/2007/Flex-and-Flash-file-uploading-...
Thu, 01/17/2008 - 17:10 — gor (not verified)
I try about 4 examples on web and try to create my self from docs. Not of them work except this.
Wed, 01/30/2008 - 05:24 — abhishek (not verified)
how to upload file and sent to Rails server .
Thu, 01/31/2008 - 00:16 — hybridhacking
Hi abhishek, I haven't worked with Ruby on Rails before, but in theory the Flex code above should work with Rails just as it does with PHP. You might want to check out this blog entry for some sample Ruby code:
Tue, 02/12/2008 - 11:52 — breathflex (not verified)
The above mentioned blog has the exact information i was looking for. Thanks guys
Thu, 05/08/2008 - 11:14 — sluzzuls (not verified)
thank you!! i almost cried when it worked :P just getting into php via flash and its killing me.
Thu, 06/05/2008 - 13:15 — John (not verified)
Thanks, i am going to use this stuff on one of my pages (personal blog).
Mon, 06/16/2008 - 09:15 — somOne (not verified)
this is great thx mate, and thx to Chris Charlton for the link aswell on how to return data from the upload. =)
Mon, 06/30/2008 - 09:59 — shotgun (not verified)
Does this work with blazeds???Pl. tell me
Wed, 07/02/2008 - 11:47 — john (not verified)
I used the above example and I can't the uploaded file on the directory where the script is.When i upload a file , "File Uploaded " text comes but I can't find the file. Pl. help
Sun, 08/31/2008 - 03:37 — Anonymous (not verified)
I am trying to retrieve a string from a session variable to use as part of the name...For some reason am am getting an empty string, does the session not start until the files have already been moved?
Wed, 10/01/2008 - 13:50 — Teknotica (not verified)
Perfect example... all what I need and very important IT WORKS! thanks!
Wed, 10/15/2008 - 21:24 — Dan (not verified)
Thank you for this tutorial!
Wed, 10/29/2008 - 07:56 — inamorty (not verified)
I owe you a beer!
Wed, 12/31/2008 - 02:59 — handoyo (not verified)
Thanks for the tutorial
Mon, 02/02/2009 - 09:55 — nodItech (not verified)
thanks, good work.
Sun, 02/08/2009 - 06:54 — vinit (not verified)
Hi...rly nice tutorial...i was searching for upload component and found here.. but can you please tell me how to upload bigger files like music files via this upload application as i am not able to do this..
Thu, 03/05/2009 - 15:35 — faisal (not verified)
i have the same problem , did you find a solution? i though it was a configuration in my php.ini, i tired the same code on other servers online, and i have the same issue,
Tue, 03/31/2009 - 19:37 — Ryan (not verified)
Will this work locally with Mamp or Wamp? It hangs on the file uploading.. part.
Fri, 04/17/2009 - 19:34 — rodo (not verified)
Great example!!!
Tue, 05/05/2009 - 12:23 — wajdi (not verified)
I have the same problem, I can't find the file despite it shows me the "file uploaded" message
Sun, 06/21/2009 - 11:21 — jai (not verified)
great tutorial ..... but what is "Filedata" in the above php code?
Mon, 09/07/2009 - 09:31 — arindam (not verified)
//fileReference.addEventListener(Event.INIT,img);
Fri, 12/18/2009 - 07:27 — Tayyaba Rasid (not verified)
Thank You for this help ,i am newbie to flex and php .I was looking for such a complete and easy code sample.
Tue, 12/29/2009 - 13:44 — Joe (not verified)
Hello, I applied your code to my Flex app that is using sun application server, i changed the serverSideScript to point to the location of the php page and i changed the mxml to be a title window instead of application and i added some try catches in the action script function the interface showed that the file has been uploaded but in reality it wasn't. Thanks in advance Regards, Joe
Thu, 12/31/2009 - 19:59 — avejidah (not verified)
Awesome dude, thanks.
Fri, 02/26/2010 - 04:58 — abhishek (not verified)
See this link for Flex with ROR File Upload Post new comment |
||||||