I have some Actionscript code that will hopefully upload multiple files to my web server (via a PHP script). A user simply selects their files by clicking on a “select” button, then uploads them by clicking “upload”. That all makes sense. To see a current version of this, goto www.pixmaker.com/fileUploader, sign-in using (username: test1, password: freedom7901), and click the active link.
However, I also want the user to fill out some text that will describe each file upload (the property name) - so I’ve also included an input text box in the flash panel, and called this instance “property_txt”. Can someone give me the correct code so that this value can be sent to the php script along with the file information? I’d like to access the value via the GET or POST function in the PHP code.
Here’s my AS code:
import flash.net.;
var currentListItem:Number;
var fullFileBytes:Number;
var progressBar:MovieClip;
var reference:FileReference;
var totalSize:Number;
var referenceList:FileReferenceList = new FileReferenceList();
var listStatus:Array = new Array();
var referenceListener:Object = new Object();
var errorFlag:Boolean = false;
var scriptLocation:String = ‘uploader.php’;
var progressBarHeight:Number = 10;
var progressBarY:Number = 80;
var progressBarColor:Number = 0x66ccff;
referenceList.addListener(referenceListener);
uploadButton_mc._visible = false;
chooseButton_mc.onRelease = choose;
uploadButton_mc.onRelease = uploadCurrent;
referenceListener.onSelect = activateUploadButton;
referenceListener.onProgress = updateProgress;
referenceListener.onComplete = tryNextUpload;
referenceListener.onHTTPError = handleError;
referenceListener.onIOError = handleError;
referenceListener.onSecurityError = handleError;
function activateUploadButton():Void {
currentListItem = 0;
totalSize = 0;
fullFileBytes = 0;
progressBar = makeProgressBar(310, progressBarY);
reference = referenceList.fileList[currentListItem];
reference.addListener(referenceListener);
uploadButton_mc._visible = true;
display_txt.text = ‘’;
for (var i:Number = 0; i<referenceList.fileList.length; i++) {
display_txt.text += referenceList.fileList.name+’
‘;
listStatus* = ‘0%’;
totalSize += referenceList.fileList*.size;
}
delete i;
}
function choose():Void {
referenceList.browse([{description:‘All Files (.)’, extension:’.’}]);
}
function handleError(errorName:String, detail:Object):Void {
errorFlag = true;
if (arguments.length === 2) {
if (typeof detail === ‘number’) {
listStatus[currentListItem] = ‘HTTP Error #’+detail;
} else {
listStatus[currentListItem] = 'Security Error: ‘+detail;
}
} else {
listStatus[currentListItem] = ‘IO Error’;
}
updateStatusText();
tryNextUpload();
}
function makeProgressBar(x:Number, y:Number):MovieClip {
var bar:MovieClip = createEmptyMovieClip(‘progressBar_mc’, 0);
bar._visible = false;
bar.beginFill(progressBarColor);
bar.lineTo(Stage.width, 0);
bar.lineTo(Stage.width, progressBarHeight);
bar.lineTo(0, progressBarHeight);
bar.lineTo(0, 0);
bar.endFill();
bar._width = 0;
bar._visible = true;
bar._x = x;
bar._y = y;
return bar;
}
function restart():Void {
removeMovieClip(progressBar);
if (errorFlag) {
errorFlag = false;
} else {
display_txt.text = ‘Success! Your files have been uploaded.’;
}
uploadButton_mc._visible = false;
chooseButton_mc._visible = true;
}
function tryNextUpload():Void {
reference.removeListener(referenceListener);
fullFileBytes += reference.size;
if (currentListItem === referenceList.fileList.length-1) {
restart();
} else {
reference = referenceList.fileList[++currentListItem];
reference.addListener(referenceListener);
uploadCurrent();
}
}
function updateProgress(fileReference:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
listStatus[currentListItem] = Math.ceil((bytesLoaded/bytesTotal)100)+’%’;
updateStatusText();
progressBar._width = Math.ceil(220((fullFileBytes+bytesLoaded)/totalSize));
}
function updateStatusText():Void {
display_txt.text = ‘’;
for (var i:Number = 0; i<referenceList.fileList.length; i++) {
display_txt.text += referenceList.fileList*.name+’ - ‘+listStatus*+’
';
}
}
function uploadCurrent():Void {
chooseButton_mc._visible = false;
reference.upload(“uploader.php?property=”+property_txt);
}
In addition, can someone take a look at my PHP script and tell me what additions I should make? I want the files to be saved in a newly created directory each time, depending on the user’s login details, and the “property” value they entered. For this example, it would be:
CURRENT DIRECTORY LOCATION -> COMPANY NAME (= username) -> BRANCH NAME (= password) -> PROPERTY
I’ve assigned $uploadDir to this. Does it look correct inthe code below?
So is my $uploaddir correct?
Here’s the code:
<?php
$company = $_SESSION[‘Company’];
$branch = $_SESSION[‘Branch’];
$property = $_GET[‘property’];
//what is the directory path I want to upload to?
$uploadDir = ‘/$company/$branch/$property/’;
//create the directory (should have write permissons)
$old_umask = umask(0);
mkdir($uploadDir, 0777);
umask($oldumask);
//move the uploaded file
$uploadFile = $uploadDir . $_FILES[‘Filedata’][‘name’];
$result = move_uploaded_file($_FILES[‘Filedata’][‘tmp_name’], $uploadFile);
chmod($uploadDir . $_FILES[‘Filedata’][‘name’], 0777);
if ($result)
{
$message = “<result><status>OK</status><message>$file_name uploaded successfully.</message></result>”;
}
else
{
$message = “<result><status>Error</status><message>Something is wrong with uploading a file.</message></result>”;
}
echo $message;
?>
Any help with any of this would be greatly appreciated. Thanks! (I have viewed many tutorials on this already, and I’m still having problems.)