Skip navigation
Currently Being Moderated

Write to text file

Jun 21, 2012 5:31 PM

Tags: #and #file #with #php #text #3.0 #actionscript #to #write

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!

 
Replies
  • kglad
    62,004 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 21, 2012 9:21 PM   in reply to libengan

    Here's my php within "saveBackup.txt"

     

    is wrong.  that needs to be a .php file.

     

    and if you don't see that php echo, you're not executing that file.

     
    |
    Mark as:
  • kglad
    62,004 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 24, 2012 8:35 PM   in reply to libengan

    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");

     

     
    |
    Mark as:
  • kglad
    62,004 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 25, 2012 8:26 AM   in reply to kglad

    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");

     

     
    |
    Mark as:
  • kglad
    62,004 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 25, 2012 12:47 PM   in reply to libengan

    then you have a typo or path problem somewhere.

     

    what's the url of that swf's embedding html?

     
    |
    Mark as:
  • kglad
    62,004 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 25, 2012 4:51 PM   in reply to libengan

    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.

     
    |
    Mark as:
  • kglad
    62,004 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 26, 2012 10:15 AM   in reply to libengan

    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.

     
    |
    Mark as:
  • kglad
    62,004 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 27, 2012 6:40 AM   in reply to libengan

    then something is wrong with your local installation of php or the way you're using it.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points