4 Replies Latest reply on May 6, 2013 11:39 PM by teeronline

    send a string to php

    teeronline Level 1

      Basically this again another new research for myself.

       

      What I'm trying to do: send out a var from as2 as a string, to a php file that would write that string down to a txt file (test.txt)

       

      I don't know how to do it, but I have this sample on hand ( which upload a file from local computer to server).

       

      What I'm trying to learn from this sample is how php grab data sending over from as2, so if anyone can point out which part in the php file that holds the name of the uploading object.

       

      very grateful,

       

      as2:

      import flash.net.FileReference;

       

      var fileRef:FileReference = new FileReference();

       

      var oListener:Object = new Object();

       

      fileRef.addListener(oListener);

       

      oListener.onSelect = function(fileRef:FileReference):Void {

          tData.text = "File Name: " + fileRef.name;

      }

       

       

      tBrowse.onPress = function() {  << tBrowse is a button

          browseFiles();

      }

      function browseFiles(): Void {

          fileRef.browse();

      }

       

      tUpload.onPress = function() {

          uploadFile();

      }

      function uploadFile():Void {

          fileRef.upload("simplefileupload.php");

      }

       

       

       

      php code "simplefileupload.php"

       

      <?php

       

      move_uploaded_file($_FILES['Filedata']['tmp_name'], './'.$_FILES['Filedata']['name']);

       

      ?>

        • 1. Re: send a string to php
          teeronline Level 1

          I have a simple fwrite php file, I just need to know what goes to $somecontent so the file name will be written on test.txt

           

          for example, $somecontent = $_FILES['Filedata']['tmp_name'], './'.$_FILES['Filedata']['name']  << will this do it????

           

          <?php

          $filename = 'test.txt';

          $somecontent = "something\n";

           

          // Let's make sure the file exists and is writable first.

          if (is_writable($filename)) {

           

              // In our example we're opening $filename in append mode.

              // The file pointer is at the bottom of the file hence

              // that's where $somecontent will go when we fwrite() it.

              if (!$handle = fopen($filename, 'a')) {

                   echo "Cannot open file ($filename)";

                   exit;

              }

           

              // Write $somecontent to our opened file.

              if (fwrite($handle, $somecontent) === FALSE) {

                  echo "Cannot write to file ($filename)";

                  exit;

              }

           

              echo "Success, wrote ($somecontent) to file ($filename)";

           

              fclose($handle);

           

          } else {

              echo "The file $filename is not writable";

          }

          ?>

          • 2. Re: send a string to php
            teeronline Level 1

            Hi, I have no idea what to google in this case, I tried a few, none comes simple explanation, that or doing something complicated. Can anyone point me to the right direction please

            • 3. Re: send a string to php
              teeronline Level 1

              Still looking for way to transfer a text from Flash to Php and have Php write that text down to test.txt.

               

              Any help will be very grateful.

               

              Thank you for reading

              • 4. Re: send a string to php
                teeronline Level 1

                Hi, I found a online tut, I have these codes on my swf, I don't why it isn't working, do I need to import some lib?

                 

                var urlReq:URLRequest = new URLRequest ("getVars.php");

                 

                submitT.addEventListener(MouseEvent.CLICK, send2php);      <<submitT is a button

                   

                function send2php(evt:MouseEvent):void {      

                    // Set the method to POST

                    urlReq.method = URLRequestMethod.POST;

                   

                    // Define the variables to post   

                    var urlVars:URLVariables = new URLVariables();

                    urlVars.userName = 'myUsername';

                    urlVars.userPass = 'myPassword';

                   

                    // Add the variables to the URLRequest

                    urlReq.data = urlVars; 

                               

                    // Add the URLRequest data to a new Loader

                    var loader:URLLoader = new URLLoader (urlReq);

                 

                    // Set a listener function to run when completed

                    //loader.addEventListener(Event.COMPLETE, onLoginComplete);

                 

                    // Set the loader format to variables and post to the PHP

                    loader.dataFormat = URLLoaderDataFormat.VARIABLES;

                    loader.load(urlReq);

                   

                }

                 

                And these on getVars.php,

                 

                 

                <?php

                    //Grab username and password variables

                    $username = $_POST['userName'];

                    $password = $_POST['userPass'];

                   

                    // for additional variables use the &

                    // success=true&username=$username&password=$password

                    echo $username;

                    echo $password;

                    echo "success=true";

                ?>