6 Replies Latest reply on Dec 6, 2010 3:58 AM by celebritymusic

    Having trouble saving .txt file from Flash

    celebritymusic Level 1

      Hi

       

      I successfully was able to save data from a Flash form to a txt file yesterday, but today NOT!  LOL - I have no idea what I've changed and done wrong, so was wondering if you guys could look?

       

      I'm using Jorge's code from Flash DB as the basics, but I can't ven get THAT to work now.

       

      Here's the as3 - I have made it so only one filed is required - "Title" - the as3 seems to be working fine:

       

      submit.addEventListener("mouseDown", sendData)


      function sendData(evt:Event){


          if(Title.text!=""){

       

              var myData:URLRequest = new URLRequest("save.php")

              myData.method = URLRequestMethod.POST

       

              var variables:URLVariables = new URLVariables()
              variables.Title = Title.text

       

              myData.data = variables
              var loader:URLLoader = new URLLoader()
              loader.dataFormat = URLLoaderDataFormat.VARIABLES
              loader.addEventListener(Event.COMPLETE, dataOnLoad)
              loader.load(myData)
          } else status_txt.text = "All fields are mandatory"
      }
      function dataOnLoad(evt:Event){
          if(evt.target.data.writing=="Ok") {
             gotoAndStop(2)      
         } else status_txt.text = "Error in saving submitted data"
      }
      stop()

       

       


      And the php:

       

      <?php
         //Capture data from $_POST array
         $title = $_POST['Title'];

       

         //Make one big string in a format Flash understand
         $toSave = $title;
         //Open a file in write mode
         $fp = fopen("anastasio.txt", "w");
         if(fwrite($fp, $toSave)) echo "writing=Ok";
         else echo "writing=Error";
         fclose($fp);  
      ?>

       

       

      I've set the permissions on my ftp folder to 777

       

      But no txt file gets written.

       

      I'm wondering if yesterday's Flash update might affect this type of operation now?

       

      Thanks for taking alook.

       

      Shaun

        • 1. Re: Having trouble saving .txt file from Flash
          kglad Adobe Community Professional & MVP

          what's echo'd back?  do you have a file named anastasio.txt in the same directory with your php file, swf file and html file?

          1 person found this helpful
          • 2. Re: Having trouble saving .txt file from Flash
            celebritymusic Level 1

            Hey kglad

             

            Nothing is getting echoed back, and I've tried running it with and without that txt file in the folder.

             

            Here's my version of the php and as3 code - I just want the text that is in the text field 'everything' to be saved in (overwrite) the txt file 'settings' (I can set up checking for an empty text field) :

             

            <?php

             

               $everything = $_POST['saveThisData'];
              
               $toSave = $everything;

             

               $fp = fopen("settings.txt", "w");
               if(fwrite($fp, $toSave)) echo "writing=Ok";
               else echo "writing=Error";
               fclose($fp);  
            ?>

             

            function sendData(evt:Event){

             

                    var myData:URLRequest = new URLRequest("save.php");
                   
                    myData.method = URLRequestMethod.POST;
                    var variables:URLVariables = new URLVariables();
                   
                    variables.saveThisData = everything.text;
                   
                    myData.data = variables;

             

                    var loadData:URLLoader = new URLLoader();
                    loadData.dataFormat = URLLoaderDataFormat.VARIABLES;
                    loadData.addEventListener(Event.COMPLETE, dataLoadedBaby);
                    loadData.load(myData);
               
            }

             

            function dataLoadedBaby(evt:Event){
               
                if(evt.target.data.writing=="Ok") {

             

                   saveSuccess.visible = true;
                   gotoAndPlay("machine");
                  
               } else saveError.visible = true;
            }

             

             

             

            Thanks for taking a look.

             

            Shaun

            • 3. Re: Having trouble saving .txt file from Flash
              jsanders1005

              i'm having the same problem. did you figure this out at some point?

              • 4. Re: Having trouble saving .txt file from Flash
                celebritymusic Level 1

                Hi jsanders

                 

                I haven't loaded this project up for a while, but here is a cut n' paste of the code that was working:

                 

                AS3:

                 

                 

                function sendData(evt:Event){

                 

                        var myData:URLRequest = new URLRequest("text_php_code.php");

                 

                        myData.method = URLRequestMethod.POST;
                        var variables:URLVariables = new URLVariables();

                  
                        var origEverything:String = "some test text";      

                 

                        variables.saveThisText = origEverything;       
                       
                        myData.data = variables;
                        var loadData:URLLoader = new URLLoader();
                        loadData.dataFormat = URLLoaderDataFormat.VARIABLES;
                        loadData.addEventListener(Event.COMPLETE, dataLoadedBaby);
                        loadData.load(myData);
                   
                }

                 

                function dataLoadedBaby(evt:Event){
                    if(evt.target.data.writing.indexOf("Ok")>-1) {
                        saveSuccess.visible = true;
                        savingXML.visible = false;
                        gotoAndPlay(4);
                   } else saveError.visible = true; savingXML.visible = false;
                }

                 

                 

                 

                 

                PHP:

                 

                <?php
                   //Capture data from Flash variable, and add it to "$everything"
                   $everything = $_POST['saveThisText'];
                   
                   //Make one big string in a format Flash understand
                   //stripslashes($everything);
                   $toSave = $everything;

                   //Open a file in write mode
                   $fp = fopen("test_text_file.txt", "w");
                   if(fwrite($fp, $toSave)) echo "writing=Ok";
                   else echo "writing=Error";
                   fclose($fp);  
                ?>

                 

                 

                 

                Of course, it's tailored to my project, but it works and may just need a little tweaking to fit into your project.

                 

                Hope it helps.

                 

                Shaun

                • 5. Re: Having trouble saving .txt file from Flash
                  jsanders1005 Level 1

                  hey shaun,

                   

                  thanks for the reply. i got it to work. i had a mistake because i was using some code from the db version on the same website...i wasn't reading carefully. thanks again.

                   

                  best,

                  james

                  • 6. Re: Having trouble saving .txt file from Flash
                    celebritymusic Level 1

                    You're welcome - glad it worked out.