Flash (ActionScript 2.0) and PHP Upload

Posted: January 21, 2011 in Flash ActionScript, PHP
Tags: , , , , , ,

This code shows the most basic functions to build a Flash photo-uploader.
A single File Uploader demonstrating file size & file type limitations with “uploading progress” check.

The live demo, JavaScript, PHP codes, and EXTRA features of this solution can be found at http://demo.programmerspride.com/flash-upload-resize-photo/

import flash.net.FileReference;
var imageFile:FileReference = new FileReference();
var listener:Object = new Object(); // The listener object listens for FileReference events
imageFile.addListener(listener);
var upload_file:Boolean;

// When button pressed to browse a File
btn_browse.onPress = function():Void {
	// Actual browsing of Files
	imageFile.browse([{description: "Image Files (*.jpg;*.JPG;*.gif;*.GIF;*.png;*.PNG)",
					   extension: "*.jpg;*.JPG;*.gif;*.GIF;*.png;*.PNG"}]);
}

// Upload the File
btn_upload.onPress = function():Void {

	if(upload_file) {

		imageFile.upload("upload.php?sfn="+ _root.session_id +"_"+ _root.filename);
	} else getURL("javascript:img.empty()");
}

// When done selecting a File
listener.onSelect = function ():Void {

	var bytes = imageFile.size;
	var unit = "B";

	if(bytes >= 1073741824) {

		bytes = (bytes / 1024 / 1024 / 1024);
		unit = "GB";
	}
	else
	if(bytes >= 1048576) {

		bytes = (bytes / 1024 / 1024);
		unit = "MB";
	}
	else
	if(bytes >= 1024) {

		bytes = (bytes / 1024);
		unit = "KB";
	}

	if(unit != "B") {

		// Round it off in 2 decimal places
		bytes = (Math.round(bytes * Math.pow(10, 2)) / Math.pow(10, 2))
	}

	if(bytes > 5 && (unit == "MB" || unit == "GB")) upload_file = false;
	else upload_file = true;

	getURL("javascript:img.fileinfo('"+ bytes +"_"+ unit + '|' + imageFile.name +"');");
}

listener.onProgress = function (imageFile:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {

	var p = Math.round((bytesLoaded / bytesTotal) * 100);
	getURL("javascript:img.progress('"+ p +"');");
}

listener.onComplete = function():Void {

	getURL("javascript:img.uploaded('"+ _root.session_id +"_"+ _root.filename +"');");
}

For file checking and debugging purposes, you can include the following lines:

listener.onHTTPError = function(imageFile:FileReference):Void {

	trace("onHTTPError: "+ imageFile.name);
}

listener.onIOError = function(imageFile:FileReference):Void {

	trace("onIOError: "+ imageFile.name);
}

listener.onSecurityError = function(imageFile:FileReference, errorString:String):Void {

	trace("onSecurityError: "+ imageFile.name +" errorString: "+ errorString);
}
Advertisements
Comments
  1. Kam says:

    Hello!

    Where cas I find the source code of upload.php?

    Thanks!

    Kam.

  2. SPS says:

    Hi Kam,

    Thanks for dropping by.

    The content of upload.php are just few lines you can have below:

    <?php
    
    move_uploaded_file($_FILES['Filedata']['tmp_name'], $_FILES['Filedata']['name']);
    echo $_FILES['Filedata']['name'];
    
    ?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s