0

My Flash movie reads and sends data to a PHP file in a free server. It seems ok for Flash to read variable values from a text file (which is managed by a PHP file) if they are wrote in this way: &variable = value&, I have no problem with that. But my PHP file, pre-treated (by some mathematical functions) data sent by Flash and then, updates the values in the text file, that is my intention but I can't accomplish it. Suppose I want to update a counter ( it counts how many times the data were updated): in the text file I have &counter=0& (initial value) and if I put in the PHP file:

<?php
$fp = fopen("jose_stats.txt", "r");// I guess with it, I've read all the variables and values
// one of them is the variable &counter.
fclose($fp);

$toSave = "&counter=&counter+1&\n";

$fp = fopen("jose_stats.txt", "w");
if(fwrite($fp, "$toSave")) { 
  echo "&verify=success&"; //prints to screen &verify=success which flash will read
                          //and store as myVars.verify
   } else { // simple if statement
      echo "&verify=fail&"; //prints to screen &verify=fail which flash will read and
                           //store as myVars.verify
     }
   fclose($fp);

?>

but then, I check my text file and it has &counter=&counter+1& line :( and not the expected &counter =1&. Please, give me and advise. Thank you.

1 Answer 1

1

Why not use JSON?

Just store the data in JSON format:

$count = 1;

$toWrite = array( 'count' => $count );//put other data into this array if you want

//encode it
$toWrite = json_encode( $toWrite );

//and now write the data

To decode it in flash, import the JSON class:

An example of JSON in as2 using the JSON.as class:

try {
  var o:Object = JSON.parse(jsonStr);
  var s:String = JSON.stringify(obj);
} catch(ex) {
  trace(ex.name + ":" + ex.message + ":" + ex.at + ":" + ex.text);
}

So just import the class, and run JSON.parse( yourPhpResponse );.

Also, the reason for why you're seeing &counter=& in the text file is because you're storing it like that: $toSave = "&counter=&counter+1&\n";.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, I will consider JSON. But I'm still asking myself if it were possible to read variable values from a text file using php code. I want to know if it does or doesn't.
It is possible but it would be a right pain. This is what JSON was designed for. To convert data into a format that most languages can easily understand.
I followed your advice and everything is running ok, just remain one more question: the json_encode( $toWrite ) statement makes a very long horizontal line of the json object contents, won't be there any problem with that when the array is very long? or in other words, is there any limit in the horizontal direction of the txt or json file?. By the way, I want to leave here a link of a nice example of code to read json contents from Flash AS2: thinkflash.blogspot.com.ar/2009/05/study.html
Glad it's working and no, there is no limit at all. You can have as much contents as you like in a text file. It's perfectly fine. Be sure to accept this answer xD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.