![]() |
|||||||
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 — shongrunden
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!
Sat, 11/15/2008 - 08:19 — elliot (not verified)
love your simple and great tutorial. Post new comment |
||||||