[COLOR="red"]Here is the PHP Script code titled upload.php[/COLOR]
<?php
if (is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
$uploadDirectory = "uploads/";
$uploadFile = $uploadDirectory . basename($_FILES['Filedata']['name']);
copy($_FILES['Filedata']['tmp_name'], $uploadFile);
}
?>
[COLOR="red"]And here is the code for the .swf file[/COLOR]
//import the FileReference Object
import flash.net.FileReference;
//initial settings - since no upload file type selet yet user cannot upload a file
uploadButn.enabled = false;
browseButn.enabled = false
//create a new FileReference object
var fileRef:FileReference = new FileReference();
//create a listener object for FileReference events
var fileRefListener:Object = new Object();
//create a listener object for comboBox events
var myComBoxListener:Object = new Object();
var isDefault:Boolean = false;
//a small function to redraw (reset) the progress bar
function reDrawPB (pb, xCor, yCor) {
_root.destroyObject(pb);
_root.createObject("ProgressBar",pb,0);
_root.progressBar.move(xCor,yCor);
}
//===================== COMBO BOX EVENT HANDLER =====================//
myComBoxListener.change = function(obj) {
fileType = obj.target.selectedItem.label
switch (fileType) {
case "Image Files":
fileDescription = "Image Files";
fileExtension = "*.jpg; *.jpeg; *.gif; *.png";
reDrawPB ("progressBar", 215.9, 128);
break;
case "Video Files":
fileDescription = "Video Files";
fileExtension = "*.mov; *.wmv; *.avi; *.mp4";
reDrawPB ("progressBar", 215.9, 128);
break;
case "Audio Files":
fileDescription = "Audio Files";
fileExtension = "*.wav; *.mp3;";
reDrawPB ("progressBar", 215.9, 128);
break;
case "Text Files":
fileDescription = "Text Files";
fileExtension = "*.txt; *.rtf; *.doc; *.fdx; *.pdf";
reDrawPB ("progressBar", 215.9, 128);
break;
case "Zip Files":
fileDescription = "Zip File";
fileExtension = "*.zip";
reDrawPB ("progressBar", 215.9, 128);
break;
default:
isDefault = true;
trace (isDefault);
break;
}
status_txt.vPosition = status_txt.maxVPosition;
if (isDefault == true) {
//if user select the file type option it will clear the text area
status_txt.text="";
browseButn.enabled = false;
isDefault = false;
}else {
status_txt.text = fileDescription + ' ' + '('+fileExtension+')'+'\n';
browseButn.enabled = true;
}
}
//apply object listener to the file reference object
fileType.addEventListener("change", myComBoxListener);
//===================== FILEREFERENCE EVENT HANDLER =====================//
//When user selects a file from the file-browsing dialog box,
//the onSelect() method is called, and passed a reference to the FileReference object
fileRefListener.onSelect = function (fileRef:FileReference):Void {
uploadButn.enabled = true;
reDrawPB("progressBar", 215.9, 128);
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text += "File is selected to upload \n";
status_txt.vPosition = status_txt.maxVPosition;
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text += "-------------------------------FILE DETAILS------------------------------- \n";
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text += "File Size: " + fileRef.size + " bytes" + '\n';
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text += "File Type: " + fileRef.type + '\n';
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text += "File Name: " + fileRef.name + '\n';
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text += "Date Created: " + fileRef.creationDate + '\n';
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text += "Date Modified: " + fileRef.modificationDate + '\n';
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text += "--------------------------------------------------------------------------------- \n";
}
//When user dismiss the file-browsing dialog box,
//the onCancel() method is called, and passed a reference to the FileReference object
fileRefListener.onCancel = function (fileRef:FileReference):Void {
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text +="User terminated file upload. \n";
}
//When the file upload/download process started,
//the onOpen() method is called, and passed a reference to the FileReference object
fileRefListener.onOpen = function (fileRef:FileReference):Void {
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text +="Opening file " + fileRef.name + '\n';
}
//The onProgress() method is called periodically during the file upload operation
fileRefListener.onProgress = function (fileRef:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
//setting the status bar function
progressBar.mode = "manual";
progressBar.setProgress(bytesLoaded, bytesTotal);
}
//When the file upload/download operation is successfully complete,
//the onComplete() method is called, and passed a reference to the FileReference object
fileRefListener.onComplete = function (fileRef:FileReference):Void {
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text += "The "+fileRef.name + " ha been uploaded.\n";
//upload is now complete, disable the upload & browse button
uploadButn.enabled = false;
}
//********************************* ERROR EVENT HANDLING *********************************//
//This method will be called if and only if an upload fails because of an HTTP error
fileRefListener.onHTTPError = function (fileRef:FileReference, error:Number) {
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text +="HTTP error: with " + fileRef.name + " :error #" + error + '\n';
}
//This method will be called if and only if a file input/output error occur
fileRefListener.onIOError = function (fileRef:FileReference) {
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text +="HTTP error: with " + fileRef.name + '\n';
}
//This method will be called if and only if an upload fails because of a security error
//9 out of 10 time this error occus is because of the user Flash8 player security settings
fileRefListener.onSecurityError = function (fileRef:FileReference, errorString:String) {
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text +="Security error: with " + fileRef.name + " : " + errorString + '\n';
}
//****************************************************************************************//
//attach Listener to the FileReference Object
fileRef.addListener(fileRefListener);
//button event for the browse button
browseButn.clickHandler = function () {
//The browse function is the key, coz it displays a file-browsing dialog box
//in which the user can select a local file to upload
fileRef.browse([{description: fileDescription, extension: fileExtension}]);
}
//Button event for the upload button
uploadButn.clickHandler = function () {
status_txt.vPosition = status_txt.maxVPosition;
status_txt.text +="Attempting to upload " + fileRef.name + '\n';
//upload the file to the PHP script on the server
//put your domain in the upload() method
fileRef.upload("http://www.mywebsite.com/uploadsystem/upload.php");
}