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!

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.
3 + 15 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
 

Copyright © 2007 Steven Shongrunden