Flash CS3 and PHP - File Upload




ActionScript 3

package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.*;
import flash.net.*;
import fl.controls.Button;

public class FileUpload extends Sprite {
private var _output:TextField;
private var _fileReference:FileReference;
var browseButton:Button = new Button();
var uploadButton:Button = new Button();
public function FileUpload() {
browseButton.move(10, 30);
browseButton.buttonMode = true;
browseButton.label = "Browse";
browseButton.addEventListener(MouseEvent.CLICK, browseHandler);
addChild(browseButton);
uploadButton.move(120,30);
uploadButton.buttonMode=true;
uploadButton.label ="Upload";
uploadButton.addEventListener(MouseEvent.CLICK, uploadHandler);
uploadButton.visible = false;
addChild(uploadButton);
_output = new TextField();
_output.width = 400;
_output.height = 400;
_output.y = 75;
addChild(_output);
_fileReference = new FileReference();
_fileReference.addEventListener(Event.SELECT, selectHandler);
_fileReference.addEventListener(Event.CANCEL, cancelHandler);
_fileReference.addEventListener(ProgressEvent.PROGRESS,progressHandler);
_fileReference.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
_fileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityHandler);
_fileReference.addEventListener(Event.COMPLETE, completeHandler);
}
private function browseHandler(event:MouseEvent):void {
_fileReference.browse();
}
private function selectHandler(event:Event):void {
_output.text = "Selected File";
_output.appendText("\nName: " + _fileReference.name);
_output.appendText("\nSize: " + _fileReference.size);
_output.appendText("\nCreated On: " + _fileReference.creationDate);
_output.appendText("\nModified On: " +_fileReference.modificationDate);
uploadButton.visible = true;
}
private function cancelHandler(event:Event):void {
_output.text = "Canceled";
}
private function uploadHandler(event:MouseEvent):void {
_fileReference.upload(new URLRequest("simpleFileUpload.php"));
}
private function progressHandler(event:ProgressEvent):void {
_output.text = "file uploading\noprogress (bytes): " + event.bytesLoaded + " / " + event.bytesTotal;
}
private function ioErrorHandler(event:IOErrorEvent):void {
_output.text = "an IO error occurred";
}
private function securityHandler(event:SecurityErrorEvent):void {
_output.text = "a security error occurred";
}
private function completeHandler(event:Event):void {
_output.text = "the file has uploaded";
uploadButton.visible = false;
}
}
}

PHP

< ? php

move_uploaded_file($_FILES['Filedata']['tmp_name'], 'uploads/'.$_FILES['Filedata']['name']);

?>

Source File - Upload_File.zip

Comments

Rana said…
Hi,
i tried with similar & also with this code. but it didn't work. I need this functionality asap. can u plz tell me why it didn't work?
it just shows that file uploaded. but actually no file was uploaded.

Thanks
Anil said…
contact me at anilkumarnd@gmail.com

Ragards

Anil

Popular posts from this blog

AS3 - Access objects from external SWF files

Flash CS3 - XML Guestbook (AS3)

Flash CS4 - 3D Rotating Menu