Originally Here: http://www.kirupa.com/forum/showthread.php?t=270441
Now with an explanation and source files.
When I started this I had no PHP knowledge; now i have very little, but still no experience. What this means is, it might not be the best way, but it works.
Here’s the AS3 that goes in the main timeline (can be converted to a class):
_save.addEventListener( MouseEvent.CLICK, saveGame );
// _save refers to a button on stage with the instance name _save
// Listen for a mouse click that will cause function saveGame to fire
_load.addEventListener( MouseEvent.CLICK, loadGame );
// _load refers to a button on stage with the instance name _save
// Listen for a mouse click that will cause function loadGame to fire
var fileRef:FileReference;
// Declare a fileReference object.
function saveGame( event:MouseEvent ):void
{
var xmldoc:XML = <file>
<player>{name1.text}</player>
<lvl>{lvl1.selectedItem.label}</lvl>
<diff>{diff1.selectedItem.label}</diff>
</file>;
// Create an XML object with data to be saved from the user input. (diff1 and lvl1 refer
// to dropdown boxes, name1 refers to a text input).
var sendScript:URLRequest = new URLRequest( "http://yourdomain/send.php" );
// Point the request to the script that will handle the XML
sendScript.data = xmldoc;
// Set the URLRequest's data property to the XML object
sendScript.contentType = "text/xml";
// And the MIME type is set appropriately for XML.
sendScript.method = URLRequestMethod.POST;
// Set the http post method to send the data
var loader:URLLoader = new URLLoader( );
// Create a URLLoader to load the request
loader.load( sendScript );
// Load the request
navigateToURL( sendScript, "_self" );
// Now open the script in the current window (will hopefully cause
// the save dialog to appear)
}
function loadGame( event:MouseEvent ):void
{
fileRef = new FileReference();
// Instantiate fileRef
var fileFil:FileFilter = new FileFilter("XML File", "*.xml;");
// Declare and Instantiate a new fileReference object.
// This will filter the type of files that display in the browse dialog box
fileRef.addEventListener(Event.SELECT, selectHandler);
// Listen for when a user selects a file in the browse dialog
fileRef.addEventListener(Event.COMPLETE, completeHandler);
// Listen for when we're done with this ****.
try
{
var success:Boolean = fileRef.browse([fileFil]);
}
catch (error:Error)
{
// Catch any browse errors.
}
}
function selectHandler(event:Event):void
{
var ul:URLRequest = new URLRequest("http://yourdomain/upload.php");
// A file was selected, now we have to call another script (theyre explained below)
try
{
fileRef.upload(ul);
// Try to upload to it using the upload method
}
catch (error:Error)
{
// Catch any upload errors.
}
}
function completeHandler(event:Event):void
{
**[COLOR=Blue]// Another way, exclusive to xml, would be to simply use the xml.load(url)
// Making it a hell of a lot simpler from here.
[/COLOR]** var script:URLRequest = new URLRequest( "http://yourdomain/content.php" );
// You should know by now
script.data = fileRef.name;
// The reason for this is we're sending the filename as it should appear
// on the server, to the script.
script.contentType = "text/plain";
// Nothing special about a simple string.
script.method = URLRequestMethod.POST;
// HTTP Post Method
var loader:URLLoader = new URLLoader( );
// Create a URLLoader to load the request;
loader.addEventListener( Event.COMPLETE, getResponse );
// Listen so we can handle the response
loader.load( script );
// Load the request
}
function getResponse( event:Event ):void
{
var gameData:XML = new XML( event.target.data );
// XML object gameData is set to what we echo in php
name2.text = gameData.player;
// Read <player> node
lvl2.text = gameData.lvl;
// Read <lvl> node
diff2.text = gameData.diff;
// Read <diff> node
}
Alright now for the PHP scripts.
For saving we use send.php which will read in the xml data via HTTP POST method, and will, using headers, output a file to save.
I had my own way of downloading files, however I found out that there are certain problems that will arise across different browsers. So I used a supposedly foolproof file download function, that I got from a friend. Not sure if he made it (doubt it), but that’s not the point. It works, I’m happy and thank the man that made it.
<?php
// Read In XML from Raw Post Data.
$xmldoc = $GLOBALS['HTTP_RAW_POST_DATA'];
file_download($xmldoc, 'data.xml', 'text/xml');
function file_download($filestring, $filename, $filetype = 'application/octet-stream')
{
//Ill just leave you to download the file since the function is very long.
}
?>
Now for the upload script. Unfortunately, the only workaround that worked for me is uploading to a server, reading the file, then deleting the file.
I tried using a more efficient way using the temporary location of the file ( $_FILES[‘Filedata’][‘tmp_name’] ) but that didn’t work. Oh well.
So this will upload the file to a directory called ‘temp’ and nothing more. I haven’t added a filesize restriction, but you can. XML files are generally very small.
<?PHP
$target_path = "temp/";
$target_path = $target_path . basename( $_FILES['Filedata']['name']);
move_uploaded_file($_FILES['Filedata']['tmp_name'], $target_path);
?>
Next we have to have a script that will run once the file upload is complete. It will simply locate the file (since we send the location from flash) then send its contents to flash, and delete the file on the server. Simple.
<?php
$name = $GLOBALS['HTTP_RAW_POST_DATA'];
$path = "temp/";
$path = $path . $name;
$contents = file_get_contents($path);
echo $contents;
unlink($path);
?>
And that’s it. Post any questions, fixes, errors, better ways of doing things, etc.
Here’s the zip.