Appending XML with Flash & PHP
Hey all,
I'm doing a church website and I'm trying to create an app where someone from the church can upload a preacher's sermon and it will be added to the mp3 player that I created (which is driven by an XML file). So, after doing some research I realized that I have to do this in PHP, but I can't get it to work.
Everything on the Flash side appears to be working properly and I'm not getting any errors, but there is no mp3 file uploaded to my server and the XML file is not updated. Can you guys help me? FYI, I'm using AS 3.0.
Here's my PHP:
PHP Code:
<?php
/*
Script Written By: Adam Khoury @ www.developphp.com
Adapted by Justin Summerlin
*/
// Set local PHP vars from the POST vars sent from flash
$todayDate = $_POST['todayDate'];
$Speaker = $_POST['Speaker'];
$Date = $_POST['sermonDate'];
$Title = $_POST['Title'];
$filename = $_FILES['Filedata']['name'];
$filetmpname = $_FILES['Filedata']['tmp_name'];
$fileType = $_FILES["Filedata"]["type"];
$fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1000);
// Place file on server, into the sermons folder
move_uploaded_file($_FILES['Filedata']['tmp_name'], "sermons/sermons/".$filename);
// This section edits your log file, if you don't need a text log file just delete these lines
$myFile = "logFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "\n\ntodayDate: $todayDate \n Name: $Name \n Email: $Email \n ssid: $ssid \n FileName: $filename \n TmpName: $filetmpname \n Type: $fileType \n Size: $fileSizeMB MegaBytes";
fwrite($fh, $stringData);
fclose($fh);
// End log file edit
//Appending to the current XML file
//Creating a new DomDocument
$xdoc = new DomDocument;
//Loading in the current XML file
$xdoc->Load('http://www.wagramchurchofgod.com/sermons/xmlTest.xml');
//getting the ROOT element (in this case, it's "songs")
$songs = $xdoc->getElementsByTagName('songs')->item(0);
//creating a new "song" element underneat the root
$newElement = $xdoc ->createElement('song');
//adding the URL attribute to the new song element
$url = $xdoc->createAttribute("url");
$newElement->appendChild($url);
$urlValue = $dom->createTextNode("sermons/".$filename);
$url->appendChild($urlValue);
//adding the PREACHER attribute to the new song element
$preacher = $xdoc->createAttribute("preacher");
$newElement->appendChild($preacher);
$preacherValue = $dom->createTextNode($Speaker);
$preacher->appendChild($preacherValue);
//adding the DATE attribute to the new song element
$myDate = $xdoc->createAttribute("date");
$newElement->appendChild($myDate);
$myDateValue = $dom->createTextNode($Date);
$myDate->appendChild($myDateValue);
//adding the TITLE attribute to the new song element
$myTitle = $xdoc->createAttribute("title");
$newElement->appendChild($myTitle);
$myTitleValue = $dom->createTextNode($Title);
$myTitle->appendChild($myTitleValue);
//adding the new element, WITH ATTRIBUTES, to the <SONGS> element in the XML file
$songs -> appendChild($newElement);
//Saving the XML file and printing the results to the $test variable
$test = $xdoc->save("http://www.wagramchurchofgod.com/sermons/xmlTest.xml");
echo "<B>Data Appended<B>";
echo $test;
?>
And here's my Actionscript (if you need it):
PHP Code:
stop();
btnSermons.addEventListener(MouseEvent.CLICK, showSermons);
function showSermons(e:MouseEvent):void
{
btnEvents.addEventListener(MouseEvent.CLICK, showEvents);
btnSermons.removeEventListener(MouseEvent.CLICK, showSermons);
Tweener.addTween(btnEvents, {x:639, time:1, transition:"easeOutQuint"})
Tweener.addTween(mcSermonsPage, {x:328, time:1, transition:"easeOutQuint"})
}
function showEvents(e:MouseEvent):void
{
btnSermons.addEventListener(MouseEvent.CLICK, showSermons);
btnEvents.removeEventListener(MouseEvent.CLICK, showEvents);
Tweener.addTween(btnEvents, {x:89, time:1, transition:"easeOutQuint"})
Tweener.addTween(mcSermonsPage, {x:-222, time:1, transition:"easeOutQuint"})
}
/*------------------------------------------------------------------------------------*/
/*
Script Written By: Adam Khoury @ www.developphp.com
adapted by Justin Summerlin
*/
// First thing is to set the flashing upload message clip to invisible
//uploadMsg.visible = false;
// Set the URL for the PHP uploader script
var url:URLRequest = new URLRequest("http://www.wagramchurchofgod.com/Scripts/uploader_script.php");
// Assign the image types Filter
var audioTypes:FileFilter = new FileFilter("Audio (*.mp3)", "*.mp3");
// Set the FileReference name
var fileRef:FileReference = new FileReference();
var toggler:Number = 0;
// Add event listeners for its various fileRef functions below
fileRef.addEventListener(Event.SELECT, syncVariables);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
//event listener for error text holder
mcSermonsPage.mcError.addEventListener(MouseEvent.CLICK, grow);
function grow(e:MouseEvent):void
{
if(toggler == 0)
{
Tweener.addTween(mcSermonsPage.mcError, {x:20.1, y:-6.4, scaleX:1, scaleY:1, time:1, transition:"easeOutQuint"});
toggler++;
}
else
{
Tweener.addTween(mcSermonsPage.mcError, {x:250.2, y:-116.4, scaleX:.1, scaleY:.1, time:1, transition:"easeOutQuint"});
toggler = 0;
}
}
// Add event listeners for your 2 buttons
mcSermonsPage.btnBrowse.addEventListener(MouseEvent.CLICK, browseBox);
mcSermonsPage.btnBrowse.buttonMode = true;
mcSermonsPage.btnDone.addEventListener(MouseEvent.CLICK, uploadVars);
mcSermonsPage.btnDone.buttonMode = true;
// Function that fires off when the user presses "browse for a file"
function browseBox(e:MouseEvent):void
{
fileRef.browse([audioTypes]);
}
// Function that fires off when the user presses the "upload it now" btn
function uploadVars(e:MouseEvent):void
{
mcSermonsPage.mcBlocker.visible = false;
//uploadMsg.visible = true;
fileRef.upload(url);
mcSermonsPage.btnDone.visible = false;
}
// Function that fires off when File is selected from PC and Browse dialogue box closes
function syncVariables(e:Event):void
{
mcSermonsPage.txtFilename.text = "" + fileRef.name;
mcSermonsPage.btnDone.visible = true;
mcSermonsPage.mcProgress.width = 1;
var variables:URLVariables = new URLVariables();
variables.todayDate = new Date();
variables.Title = mcSermonsPage.txtTitle.text; // Title of the sermon
variables.Speaker = mcSermonsPage.txtSpeaker.text; // the person who preached the sermon
variables.sermonDate = mcSermonsPage.txtDate.text; // date of sermon
url.method = URLRequestMethod.POST;
url.data = variables;
}
// Function that fires off when upload is complete
function completeHandler(e:Event):void
{
//uploadMsg.visible = false;
mcSermonsPage.mcBlocker.visible = true;
mcSermonsPage.txtStatus.text = "Your file has been uploaded successfully.";
mcSermonsPage.txtFilename.text = "";
}
// Function that fires off when the upload progress begins
function progressHandler(e:ProgressEvent):void
{
// we want our progress bar to be 200 pixels wide when done growing so we use 200*
// Set any width using that number, and the bar will be limited to that when done growing
mcSermonsPage.mcProgress.width = Math.ceil(200*(e.bytesLoaded/e.bytesTotal));
mcSermonsPage.txtPercent.text = String(Math.ceil(100*(e.bytesLoaded/e.bytesTotal))) + "%";
}