Subscribe to our newsletter:
Flash has long lacked the ability to upload files, a standard function in programming languages. Kris Hadlock cheers the addition of the FileReference object in Flash 8; by combining a bit of ActionScript and PHP, it's now quite simple to create a Flash file-uploading GUI.
The FileReference class is a long-awaited addition to Flash. This class allows you to browse local drives on a user's computer and provide the user with the ability to select files for upload by sending the file to a simple server-side script.
This article assumes that you have an understanding of ActionScript and basic knowledge of PHP. The sample download contains a fully functional, Flash file-uploading GUI with a PHP back-end to transport the files. I'll show you how to write the ActionScript to handle the front-end FileReference code. The FileReference code will handle all possible situations, from canceling the upload to sending the file to a server-side script. Once we cover the ActionScript, I'll show you a simple server-script for handling the upload and actually moving a file to the server.
How to Code the ActionScript
The FileReference class offers a robust set of methods and properties that cover many user interactions. In this section, we'll see how to use this functionality to handle the front-end code. Before we can use the FileReference class, we must first import it:import flash.net.FileReference;var fileTypes:Array = new Array();
var imageTypes:Object = new Object();
imageTypes.description = "Images (*.jpg, *.jpeg, *.gif, *.png)";
imageTypes.extension = "*.jpg; *.jpeg; *.gif; *.png";
fileTypes.push(imageTypes)Now let's add a way to trigger uploads and create an instance of the FileReference object. The sample uses a button to handle uploads and requires a listener object to handle the events. When the button is clicked, the listener fires the click event and creates a new FileReference object. The FileReference object then registers its own listener to listen for selected files, fires the browse method, and opens a file-browsing dialog. The file-browsing dialog takes the fileTypes array that we created earlier as a parameter.
var fileListener:Object = new Object();
var btnListener:Object = new Object();
btnListener.click = function(eventObj:Object)
{
var fileRef:FileReference = new FileReference();
fileRef.addListener(fileListener);
fileRef.browse(fileTypes);
}
uploadButton.addEventListener("click", btnListener);fileListener.onCancel = function(file:FileReference):Void
{
resultsTxtField.text += "File Upload Cancelledn";
}fileListener.onSelect = function(file:FileReference):Void
{
resultsTxtField.text += "File Selected: " + file.name;
if(!file.upload("http://www.your domain.com/path to/upload.php"))
{
resultsTxtField.text += "Upload dialog failed to open.n";
}
}fileListener.onOpen = function(file:FileReference):Void
{
resultsTxtField.text += "File Opened: " + file.name + "n";
}fileListener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void
{
resultsTxtField.text += "Loading Progess: bytesLoaded: " + bytesLoaded + ", bytesTotal: " + bytesTotal + "n";
}fileListener.onComplete = function(file:FileReference):Void
{
resultsTxtField.text += "File Upload Complete: " + file.name + "n";
}How to Write the PHP
The PHP for handling the file upload is a very small and simple script. (Note that no security is added to the script; I'm leaving that part to you.) The method that we use to move the file to a specific location on the server is move_uploaded_file. This method takes two parameters: a string filename representing the selected file, and a string destination representing the server location where we want the file to be uploaded.move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$_FILES['Filedata']['name']);chmod("./files/".$_FILES['Filedata']['name'], 0777);




help
and feedback
latest
news
latest
forum entries