Save XML and text files from Flash to your HD with AS3 and PHP

In this post I’ll show how to save a file from AS3 to your local HD. In another post I’ve explained how to load a file from HD to Flash.

For a project I’ve been looking around for a decent solution to the problem of saving a text file (to be more precise XML file) from Actionscript 3 to my local harddisc but i couldn’t find a solution for this (what not necessarily means that there is none).

Saving and loading can be smoothly achieved with Adobe Air but not with Flash because Flash has been designed to run on the web. However, it is possible to download and upload files via AS3 on and from your HD to and from a webserver. Think of an Flash MP3 shop: You can click on an MP3 in Flash and you will be asked where to save the file and afterwards the file is being downloaded to the chosen location on your HD. So it must be somehow possible.

Additionally, it is possible to create XML or other text in Flash and store it on a webserver and vice versa.

Thus I came upon following solution:

Howto save and load text files with AS3

Figure 1: Howto save and load text files with AS3



I first upload my text file to a server in my AS3 code (1. Upload via PHP in figure 1):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var serverUrl:String = "http://your-domain.com/pathToPHPScript/";
var xmlSaveScriptUrl:String = serverUrl + "scriptName.php";  
var fileUrl:String = serverUrl+"remoteFileName.xml";
var request:URLRequest = new URLRequest();
request.url = xmlSaveScriptUrl;
request.method = URLRequestMethod.POST;
var requestVariables:URLVariables = new URLVariables();
//1st POST variable: filename
requestVariables.filename = "remoteFileName.xml";
//2nd POST variable: xmlcontents, your text file!
requestVariables.xmlcontents = saveXML;
request.data = requestVariables;
//sendToURL is built-in function that just sends a request
sendToURL(request);

The according PHP script http://your-domain.com/pathToPHPScript/scriptName.php could look like this (still 1. Upload via PHP in figure 1):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php 
#1st POST variable
$fileName = $_POST["filename"];
#2nd POST variable
$xmlContents = $_POST["xmlcontents"];
#remove backslashes from xml string (skip this for plain text)
$lastBackslashPos = strpos ($xmlContents, "\\");
while($lastBackslashPos >0){
   $xmlContents = substr($xmlContents,0,$lastBackslashPos)
      .substr($xmlContents,$lastBackslashPos+1,strlen($xmlContents));
   $lastBackslashPos = strpos ($xmlContents, "\\");
}
#write xml data to file on server
$fh = fopen($fileName, "w");
fwrite($fh, $xmlContents);
fclose($fh);
?>

Now you’ve got the file saved on the server and can download it to your HD via AS3 (2. Download via AS3 FileReference in figure 1):

1
2
3
4
5
6
7
8
9
var request:URLRequest = new URLRequest();
request.url = fileUrl;
request.method = URLRequestMethod.GET;
var fileToDownload:FileReference = new FileReference();
try{
   fileToDownload.download(request, "defaultFileName.xml");
}catch (error:Error){
   trace("Unable to download file.");
}

fileToDownload.download(request, “defaultFileName.xml”) presents you with a file chooser dialog with the preset file name defaultFileName.xml where you can select the download location. When you press OK the file is downloaded to your HD.

Posted in Actionscript, Flash Tagged with: Actionscript, as3, Flash, load, save, xml
  • Joel

    Thanks so much, this works well and fits my application needs.

    – Joel

  • AJ

    Hey im having an error trying to run the php code.. i did everything you did.. but i changed

    sendToURL(request); TO navigateToURL(request);

    to see how the servers responds to the request and it says…

    Warning: fopen() [function.fopen]: Filename cannot be empty in /home/a7919461/public_html/beta/xmlphp/createXML.php on line 14
    Warning: fclose(): supplied argument is not a valid stream resource in /home/a7919461/public_html/beta/xmlphp/createXML.php on line 16

    So i don’t know if i shouldn’t have changed the sendToURL command.. or the server just won’t support FULL php…

    i’m sorry could you help me on this?

    PD: saveXML is….

    var saveXML:XML;

    ….right?

  • it looks like your post variables are not coming through from your as3 code:

    //AS3 code
    requestVariables.filename = “remoteFileName.xml”;\
    requestVariables.xmlcontents = saveXML;

    //PHP code
    $fileName = $_POST[“filename”];
    $xmlContents = $_POST[“xmlcontents”];

    $fh = fopen($fileName, “w”); //line 14 Filename cannot be empty
    fwrite($fh, $xmlContents); //line 16 argument is not a valid stream resource

    did you submit those variables in the request variable of navigateToURL(request);

    i cannot see another error source in the trace you have sent me…

    cheers

  • NewbieCoder

    Hi need some help for this, new to php and as3.

    Have a XML file which contains reports… I want the user to be able to be able to add reports to this XML file without seeing the file itself.
    So for the user they will see a Flex/HTML (or whatever) form with inputFields for Report Name, and report description as well as a drop down box for category.

    Once a Submit button is pressed, the nodes will be added to the bottom of the XML file within the root nodes, with the values of reportname, description and category set according to the form they submitted.