PDA

View Full Version : Wrtie to textfile as/php stops working in nested Movie Clip


zenkert
11-10-2008, 01:25 PM
Hi,

I have worked with Flash and php to open and print text collected from variables in Flash to a text file.
It all works just fine from the beginning when everything happens in the
main timeline "_root" of the flash file.

But when I move the code to a new Movie Clip and add that Movie Clip to the timeline of the "_root" - timeline it stops working
I have tried several approaches without success.
The write does not take place.
And even stranger... if I do a trace on all the variables. They ALL trace correctly.

I guess it´s all on the flash side. Since I cannot see that the php file could
have anything to do with this.

This is the code if anyone please would have a look at it and point
me in the right direction. I have excluded a lot of variables just for
readabilty

//
///////// !START - Create the LoadVars Objects /////////
//
var read_lv:LoadVars = new LoadVars();
var write_lv:LoadVars = new LoadVars();
//
///// !END - Create the LoadVars Objects ! /////

//
///// !START - automatically sending write variables(s) to php script /////
//
write_lv.somecontent = ("Interaktiv examination uförd\r\n" + year + month + date + "\r\n");
write_lv.onLoad = function(success)
{
if (success) {
//trace("text written to the text file:\n" + this.message);
content_txt.text = this.message;
content_txt.text = "Text added : \r\n" + this.message;
} else {
content_txt.text = "An error has occured adding text to the file test.txt";
trace("An error occurred");
}
};
write_lv.sendAndLoad("writetofile.php", write_lv, "POST");
//
///// !END - automatically sending write variables(s) to php script /////

Jonnie

zenkert
11-10-2008, 02:17 PM
Hi,

I have worked with Flash 8 and php to open and write text collected from variables in Flash to a text file.
It all works just fine from the beginning when everything happens in the main timeline "_root" of the flash file.

But when I move the code to a new Movie Clip and add that Movie Clip to the timeline of the "_root" - timeline it stops working
I have tried several approaches without success.
The write does not take place.
And even stranger... if I do a trace on all the variables in Flash, they ALL trace correctly.

I guess it´s all on the Flash side. Since I cannot see that the php file could have anything to do with this.

This is the code that works on the Main Time Line but not in the nested Movie Clip - if anyone please, would have a look at it and point me in the right direction. I have excluded some of the variables just for readabilty

//
///////// !START - Create the LoadVars Objects /////////
//
var write_lv:LoadVars = new LoadVars();
//
///// !END - Create the LoadVars Objects ! /////

//
///// !START - automatically sending write variables(s) to php script /////
//
write_lv.somecontent = ("Interaktiv examination uförd\r\n" + year + month + date + "\r\n");
/*write_lv.onLoad = function(success)
{
if (success) {
//trace("text written to the text file:\n" + this.message);
content_txt.text = this.message;
content_txt.text = "Text added : \r\n" + this.message;
} else {
content_txt.text = "An error has occured adding text to the file test.txt";
trace("An error occurred");
}
};*/
write_lv.sendAndLoad("writetofile.php", write_lv, "POST");
//
///// !END - automatically sending write variables(s) to php script /////

Tossing in the php code anyway just in case...


<?php
// the name of the text file
$filename = 'test.txt';

// get the variables from flash
$somecontent = $_REQUEST['somecontent'];

// If the file doesn't exist, attempt to create it
// and open it for writing
// anything already writen will be replaced witgh settings to'w' - 'a' starts writing at the end of the file
if (!$handle = fopen($filename, 'a')) {
echo "&message=Cannot open file&";
exit;
}

// Write $somecontent to the opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "&message=Cannot write to file&";
exit;
}
// close the file
fclose($handle);

// send the variable to flash
echo "&message=$somecontent&";

exit;

?>


Jonnie

PS I cross-published this (Newbie to forums) Sorry it won´t happen again DS.

inhan
11-10-2008, 04:51 PM
Your code seems correct and it is a php issue if you can get the below trace right. Otherwise it is a scoping issue (change the paths of variables 'year', 'month' and 'date' accordingly).

write_lv.somecontent = "Interaktiv examination uförd\r\n" + year + month + date + "\r\n";
trace("write_lv.somecontent = " + write_lv.somecontent);
write_lv.onLoad = function(success) {
if (success) {
//trace("text written to the text file:\n" + this.message);
content_txt.text = this.message;
content_txt.text = "Text added : \r\n" + this.message;
} else {
content_txt.text = "An error has occured adding text to the file test.txt";
trace("An error occurred");
}
};
write_lv.sendAndLoad("writetofile.php", write_lv, "POST");
}

zenkert
11-10-2008, 06:54 PM
Inhan,

Thanks, it feels a bit better. I have checked the date and the issue is not there. In fact I have discovered something else. I do not think the php is the issue either.

<?php
// the name of the text file
$filename = 'test.txt';

// get the variables from flash
$somecontent = $_REQUEST['somecontent'];

// If the file doesn't exist, attempt to create it
// and open it for writing
// anything already writen will be replaced witgh settings to'w' - 'a' starts writing at the end of the file
if (!$handle = fopen($filename, 'a')) {
echo "&message=Cannot open file&";
exit;
}

// Write $somecontent to the opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "&message=Cannot write to file&";
exit;
}
// close the file
fclose($handle);

// send the variable to flash
echo "&message=$somecontent&";

exit;

?>


I´ve got it to work with at least three completely different solutions now. So the nested Movie Clip is not the issue any more.

I have built this as a prototype to be used in a another Flash file which has been handed to me - to fix. And now the problem is related to that file.

No matter what solution I select - all perfectly functional in my prototype - it DOES not work in the file I´m trying to fix. This file is full of script - and since I´m rather new to this I am a bit lost. Trying to get hold of the orginal creator of the file - but have not succeeded yet.

Really hope I do otherwise I´m really lost here.

Jonnie

inhan
11-10-2008, 09:10 PM
Can you make all this work in a new file? Are you sure it normally works on that server?

zenkert
11-11-2008, 09:29 AM
Yes, I can make it work in a new file.
The new file works fine both on my local WAMP server, my local XAMPP server and uploaded to the targeted remote Linux server.

The original file works fine on both local servers and on the Linux server. My protype just adds some extended functionality to the original file which extracts variable values and writes them to the text text file.
If I add the prototype functionality - the original file is still fully functional - but my additions does not work.

Jonnie

zenkert
11-11-2008, 12:08 PM
Inhan,

As you say "it's all about patience" and I would like to add "persistency".
After numerous new attempts I discovered something looking at the html-file that holds the original Flash-file. There is a preload of another Flash-file. That file loads the file I´m trying to fix.

So basically it was all about paths. When I called for "_root" - I was actually calling the preloaded swf not as I thought the original file I was trying to fix. Still some things here I don´t understand but...

Anyway I made a "loadMovie command" loading an new external file containing all my scripts with all paths relative to the correct swf - the one loaded by the preloaded swf - and now I´ve got it to work.

I would like to thank you, Inhan, because you´re guidance and questions have helped me going in the right direction. :)

Jonnie

inhan
11-11-2008, 12:19 PM
No problem, I'm glad it's solved ;)
By the way the _lockroot method is a handy one, just in case you ever need it.
It locks the _root reference in a timeline it is written, and you reach upper heights using either relative paths like _parent._parent.etc or absolute ones like _level0 or _level0.someother_mc

this._lockroot = true;

zenkert
11-11-2008, 12:29 PM
Oh,,,,I get it the original message has been moved to the correct forum.

Jonnie