Hi all,
I'm having some trouble using actionscript 3.0 and php to read and write to a text file.
Here's my actionscript:
function saveBackup(textFile:String):void {
trace("sending text");
var var_load:URLLoader=new URLLoader();
var URL_request:URLRequest;
URL_request = new URLRequest("saveBackup.php");
URL_request.method=URLRequestMethod.POST;
var URL_vars:URLVariables=new URLVariables("txtFile="+textFile);
URL_vars.dataFormat=URLLoaderDataFormat.TEXT;
URL_request.data=URL_vars;
var_load.load(URL_request);
var_load.addEventListener(Event.COMPLETE, sendComplete);
}
function sendComplete(e:Event):void {
trace("text sent");
var saveStatus=new URLVariables(e.target.data);
trace("echo is: "+saveStatus.reply);
rssLoader = new URLLoader();
rssLoader.load(new URLRequest("soap.txt"));
rssLoader.addEventListener(Event.COMPLETE,enterHandler);
}
function enterHandler(e:Event):void {
trace("checking text file");
trace("this txt file contains: "+e.target.data)
}
saveBackup("soap.txt");
Here's my php within "saveBackup.txt":
<?php
echo "reply=testecho";
$dataContent = 'urgh';
$file = $_POST['txtFile'];
$writeVar = fopen($file,'w');
fwrite($writeVar,$dataContent);
fclose($writeVar);
?>
Before being written, the file "soap.txt" conatins:
this file has not yet been written.
Here's the output when I run the actionscript:
sending text
text sent
echo is: undefined
checking text file
this txt file contains: this file has not yet been written.
For some reason, it seems that "soap.txt" is never being written, but I'm not getting any error codes from the php. I can't even get the "echo" in the php file to work and have it return to the actionscript output.
Any help would be greatly apppreciated. This is driving me nuts!
Thanks in advance!
I meant to say "saveBackup.php". That was a type when I wrote this thread. It actually is a .php file.
The echo is coming back "undefined" as seen in the actionscript output. Why would the file not be executing? I have other php files that are executing just fine. I've written and rewritten this code multiple times, and haven't a clue where I'm going wrong.
Thanks!
use the trace function to start debugging:
function saveBackup(textFile:String):void {
trace("sending text",textFile);
var var_load:URLLoader=new URLLoader();
var URL_request:URLRequest;
URL_request = new URLRequest("saveBackup.php");
URL_request.method=URLRequestMethod.POST;
var URL_vars:URLVariables=new URLVariables("txtFile="+textFile);
URL_vars.dataFormat=URLLoaderDataFormat.TEXT;
URL_request.data=URL_vars;
var_load.load(URL_request);
var_load.addEventListener(Event.COMPLETE, sendComplete);
}
function sendComplete(e:Event):void {
trace("text sent");
var saveStatus=new URLVariables(e.target.data);
for(var s:String in saveStatus){
trace(s,saveStatus[s]);
}
rssLoader = new URLLoader();
rssLoader.load(new URLRequest("soap.txt"));
rssLoader.addEventListener(Event.COMPLETE,enterHandler);
}
function enterHandler(e:Event):void {
trace("checking text file");
trace("this txt file contains: "+e.target.data)
}
saveBackup("soap.txt");
Here's the output after adding the traces you suggested:
sending text soap.txt
text sent
<?php
echo "reply testecho";
$dataContent = 'urgh';
$file = $_POST['txtFile'];
$writeVar = fopen($file,'w');
fwrite($writeVar,$dataContent);
fclose($writeVar);
?>
checking text file
this txt file contains: this file has not yet been written.
I don't even know how to continue debugging from here... I'm all out of ideas.
Thanks again!
use:
function saveBackup(textFile:String):void {
trace("sending text",textFile);
var var_load:URLLoader=new URLLoader();
var URL_request:URLRequest;
URL_request = new URLRequest("saveBackup.php");
URL_request.method=URLRequestMethod.POST;
var URL_vars:URLVariables=new URLVariables("txtFile="+textFile);
URL_vars.dataFormat=URLLoaderDataFormat.TEXT;
URL_request.data=URL_vars;
var_load.load(URL_request);
var_load.addEventListener(Event.COMPLETE, sendComplete);
}
function sendComplete(e:Event):void {
trace("text sent");
var saveStatus=e.target.data;
for(var s:String in saveStatus){
trace(s,saveStatus[s]);
}
rssLoader = new URLLoader();
rssLoader.load(new URLRequest("soap.txt"));
rssLoader.addEventListener(Event.COMPLETE,enterHandler);
}
function enterHandler(e:Event):void {
trace("checking text file");
trace("this txt file contains: "+e.target.data)
}
saveBackup("soap.txt");
The html file is in the same directory as the swf. I didn't have this particular swf posted to the web yet, but when you asked for a url, I thought I might as well post it, and see what happens. Well, it works on the live web!
http://www.liveintense.com/testing/
There is a text box in the top of the stage that shows the contents of the text, file and it indeed was rewritten to "urgh".
The question is, why can't I get it to work locally? It's pretty useless to preview the file and have it not work on my local machine, only to have it work on the web.
Does this shed any light on what the issue may be?
Thanks.
do you have php installed and configured for your local computer? do you your local files have security clearance to cross sandboxes? ( http://www.macromedia.com/support/documentation/en/flashplayer/help/se ttings_manager04.html )
p.s. you should mark message 6 as the correct or helpful answer because it solved the only issue that was in your original code.
I wasn't event aware I had to install and configure php on my locall machine. I've dabbled with it in the past and hadn't run into this problem, so it didn't event occur to me.
I installed the apache web server, mysql, and php, and configured each to play nicely. I ran the installation tests to confirm successful installation and initializations, and restarted my machine. I also added my local root folder to the allowed sites in the flash player settings to allow cross sandbboxes in this directory.
However, even after all that, I'm still getting the same flash output when testing locally and the .txt is no being written.
sending text soap.txt
text sent
checking text file
this txt file contains: this file has not yet been written.
You've been grand kglad. I'd just love to get this working locally so that I can test my applications without having to go live on the web.
Thanks!
go here: http://www.macromedia.com/support/documentation/en/flashplayer/help/se ttings_manager04.html
grant the directory with your flash files trusted status. retest.
North America
Europe, Middle East and Africa
Asia Pacific