Hybrid Hacking

Subscribe

 

Hybrid Hacking provides tutorials on how to integrate with some of today's hottest web services.

 

 

Hybrid Hacking

 

Links

RIApedia
Ryan Stewart
AllFacebook
TechCrunch

 BlogTutorialsAbout 

Uploading Files from Flex using PHP

Uploading Files from Flex using PHPThis tutorial demonstrates how to upload a local file to a remote web server using Flex and PHP. Flex provides the UI for a user to choose a file and then sends that data to a PHP script on the server side to do the processing.

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.

Uploading Files with Flex using PHP

Comments

Great tutorial,
thanks.

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-...

I try about 4 examples on web and try to create my self from docs. Not of them work except this.
Great tutorial because is simple. Thanks.

how to upload file and sent to Rails server .

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:
http://mindrulers.blogspot.com/2007/04/file-upload-in-flex-rubu-on-rails...

The above mentioned blog has the exact information i was looking for. Thanks guys

thank you!! i almost cried when it worked :P just getting into php via flash and its killing me.

Thanks, i am going to use this stuff on one of my pages (personal blog).

this is great thx mate, and thx to Chris Charlton for the link aswell on how to return data from the upload.

=)

Does this work with blazeds???Pl. tell me

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

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?

Perfect example... all what I need and very important IT WORKS! thanks!

Thank you for this tutorial!

I owe you a beer!

Thanks for the tutorial

thanks, good work.

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..

i have the same problem , did you find a solution?

i though it was a configuration in my php.ini,
but it wasn't,

i tired the same code on other servers online, and i have the same issue,
WHERE are the files i UPLOAD??
they are not where they are supposed to be.

Will this work locally with Mamp or Wamp? It hangs on the file uploading.. part.

Great example!!!

I have the same problem,

I can't find the file despite it shows me the "file uploaded" message

great tutorial ..... but what is "Filedata" in the above php code?
How it comes or where it is declared?

//fileReference.addEventListener(Event.INIT,img);
//fileReference.addEventListener(IOErrorEvent.IO_ERROR, onUploadIoError);
//fileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadSecurityError);

Thank You for this help ,i am newbie to flex and php .I was looking for such a complete and easy code sample.

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

Awesome dude, thanks.

See this link for Flex with ROR File Upload
http://abhisheksoft.wordpress.com/2009/06/19/9/
:)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
6 + 11 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
 

Copyright © 2007