PDA

View Full Version : writeing to Text File


spriggan
04-03-2003, 07:46 PM
I've got a game that I want to keep track of the highscore. I realise with MX that you can't write to an outside text file (unless it's a cookie but I want to keep this serverside). I know how to read in txt files into flash and how to send varables to PhP. But how do I use PhP to edit the text file?

freddycodes
04-03-2003, 08:25 PM
Start here.
http://www.php.net/fopen

For replacing contents:

$fp = fopen("what.txt", "w");
fputs($fp, $datatowrite);
fclose($fp);


For appending

$fp = fopen("what.txt", "a");
fputs($fp, $datatowrite);
fclose($fp);

spriggan
04-03-2003, 08:28 PM
Thanks

JacK Barron
04-08-2003, 10:49 AM
Never tried appending data
but I worked a long time with a similar php script for writing.

What i've discovered is that you cant post more than (about)1200 bytes at once.
Say simply, your flash button containing the "getURL" code for sending variable won't simply work !!! (indeed, 1st tried with LoadVar action, but the same happens)

if you send less it's ok.

If you send the same amount of data using simple php/html, it will work !

I've read a lot of tutorials about this way of storing datas : never encountered this warning ...

a) do you experience the same ?
b) what is the solution ?

Thanks in advance !

CyanBlue
04-08-2003, 11:13 AM
Interesting... ;)

I just did a quick test and I was able to send 65534 characters from Flash to PHP... See if for yourself if you are too bored... :DwriteLVs = new LoadVars();
writeLVs.output = "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz
...
...
...
65534 character long string goes here within the quotes";
writeLVs.onLoad = function ()
{
trace("Done...");
}
writeLVs.sendAndLoad("http://localhost/writeTest.php", writeLVs, "POST");<?php
$output = $_POST['output'];
$FileName = "./deleteMe.txt";
@unlink($FileName);
$FilePointer = fopen ($FileName, "w");
echo ($output);
fwrite ($FilePointer, $output);
fclose ($FilePointer);
?>Strange thing is that I don't get to see this ActionScript getting executed when the string is 65535 characters long... But if it is 65536 characters long, I get to see 2426 lines of 'Target not found: Target="false" Base="_level0"' in the output window...

Do I have too much time and am I too bored??? :D

JacK Barron
04-08-2003, 11:52 AM
Your solution works !
By jove, I don't understand why ...

______________________________
Say : I was using on a button this

on (release) {
ran = random(12000);
store = escape(store);
getURL("http://127.0.0.1/writetxt/addfla.php?filename=texte.txt&store="+store+"&"+ran, "_self", "POST");
}

and

$newfile = fopen($filename, "w");
if (!$newfile){
echo "no existing file";
exit;
}

$store .= '&ok=1';
$store = stripslashes($store);
fwrite($newfile,$store);

fclose($newfile);
echo "<BR><BR>All done!";
?>

I suspect :
my POST and the LoadVar weren't "real" post, but were understood as "get" since my request was built without "POST" (even if I add the param to this flash action)

I have to really get into this LoadVar object more deeply.

BTW : you say you couldn't write more than 64k ?
have you patched your internal flash player exporter to r79
(it's one of the point of the tech note, previous version of flashplayer were unable to deal with more than 64k ...)

Thanks Again !

CyanBlue
04-09-2003, 02:39 AM
My guess is this...

getURL() function uses URL passing through the browser, and there is some size limitation with it... (Don't get my word for it... I just remember reading it somewhere... :D) So, your getURL() call gets cut off at that size you have mentioned... Other than that, I have no idea... :D

Oh, your line, ran = random(12000);... Do you use this to solve the some sort of cache problem??? Use getTimer() function instead if you were... My line should be read like this... (I forgot to add this cuz I was just typing it in hurry...)writeLVs.sendAndLoad("http://localhost/writeTest.php?uniqueID=" + getTimer(), writeLVs, "POST");If you use random() function, there is a chance that you will get the same number more than twice, whereas getTimer() will return the unique number every time you execute the script... ;)

As for the Flash Player version, I have 6.0.79 installed...
This command, trace(getVersion());, shows me this... WIN 6,0,79,0

3brand
12-16-2004, 12:30 AM
It is working for me. Thank you SO much for figuring this one out. I do, however, have a quick question dealing with (PHP side?) appending to a file.

www.catwoodstudios.com/cms/cms.swf
www.catwoodstudios.com/cms/news.xml
www.catwoodstudios.com/cms/flash_to_xml.php

The cms.swf outputs to the flash_to_xml.php which outputs to the news.xml file. Within the flash file I added on the XML tags. Is there anyway to make the newest submittions come to the top of the XML file and all the old stuff go below it?

Thank you VERY much!

-3brand :D

CyanBlue
12-16-2004, 02:38 AM
Well... I don't really know or can tell you what you need to do without seeing the code... All I can say is that you need to somehow reverse the loop in PHP that writes the actual content... :)

3brand
12-16-2004, 02:56 AM
I mangaged to fiddle around in Flash and get an effect that takes the current XML file, puts it in a variable, then appends it to the newest entry. Here you go (sorry for not including the code, not sure how all this works):


on(release){

news_xml = new XML();
news_xml.ignoreWhite = true;
news_xml.onLoad = function(sucess) {
if (sucess) {
processNews(news_xml);
}
};
news_xml.load('http://www.catwoodstudios.com/cms/news.xml');
function processNews(xmlDoc_xml) {
stored_xml = xmlDoc_xml.firstChild.firstChild.nodeValue;
for (a in xmlDoc_xml.firstChild.childNodes) {
stored_xml = xmlDoc_xml.firstChild.childNodes[a];
}
return stored_xml;
}

writeLVs = new LoadVars();
writeLVs.output = "<submission><title>"+title_cms+"</title><date>"+date_cms+"</date><content>"+content_cms+"</content></submission>"+stored_xml;
writeLVs.onLoad = function ()
{
trace("Done...");
}
writeLVs.sendAndLoad("http://www.catwoodstudios.com/cms/flash_to_xml.php?uniqueID=" + getTimer(), writeLVs, "POST");
}


The XML output is flawed, however, since it always ends with an "undefined". Plus tags that end and start on each other. I haven't tried reading the XML file yet but thats coming after I get this. Heres the current XML file:


<submission>
<title>Test</title>
<date>20:47 - Wednesday, December 15, 2004</date>
<content>Test</content>

</submission>undefined<submission>
<title>Test 2</title>
<date>20:48 - Wednesday, December 15, 2004</date>
<content>Test again</content>
</submission>
undefined


And finally, I'm using this PHP script:


<?php
$fp = fopen("news.xml", "a");
fputs($fp, $output);
fclose($fp);
?>


If you have any ideas on how to get the XML to format properly or get rid of that undefined thing at the end of the XML doc, please tell me :) . I figure this is probably a pretty inefficient way of doing things, but, its working so far and probably will work for a while so long as the news.xml file gets dumped regularly. Thanks a bunch!

CyanBlue
12-16-2004, 03:00 AM
Well... That undefined comes from this variable, stored_xml... I don't know why you have that variable, but make sure you are assigning the correct value to it before you write the XML file...

3brand
12-16-2004, 03:08 AM
EDIT!

Ok, heres the coding in the Flash file:


on(release){
writeLVs = new LoadVars();
writeLVs.output = "<submission"+date_xml+"><title>"+title_cms+"</title><date>"+date_cms+"</date><content>"+content_cms+"</content></submission"+date_xml+">";
writeLVs.onLoad = function ()
{
//trace("Done...");
}
writeLVs.sendAndLoad("http://www.catwoodstudios.com/cms/flash_to_xml.php?uniqueID=" + getTimer(), writeLVs, "POST");
}

This is the PHP file:

<?php
$fp = fopen("news.xml", "a");
fputs($fp, $output);
fclose($fp);
?>


This is what I'm getting as output (XML):

<news><submission2215December152004><title>Test1</title><date>22:15 - Wednesday, December 15, 2004</date><content>Test1</content></submission2215December152004></news><news><submission2216December152004><title>Test2</title><date>22:16 - Wednesday, December 15, 2004</date><content>Test2</content></submission2216December152004></news>


Please goto:
http://www.catwoodstudios.com/cms/news.xml

Go to the above link to see the error message I'm getting. Any ideas? Thanks a bunch!

CyanBlue
12-16-2004, 03:00 PM
Well... I don't know what's going on, but your XML file looks like this from that link...
<news>
<submission2215December152004>
<title>Test1</title>
<date>22:15 - Wednesday, December 15, 2004</date>
<content>Test1</content>
</submission2215December152004>
<submission2216December152004>
<title>Test2</title>
<date>22:16 - Wednesday, December 15, 2004</date>
<content>Test2</content>
</submission2216December152004>
</news>
<submission2308December152004>
<title>Test3</title>
<date>23:08 - Wednesday, December 15, 2004</date>
<content>Test 3</content>
</submission2308December152004>
<submission0845December162004>
<title>Test</title>
<date>08:45 - Thursday, December 16, 2004</date>
<content>Test</content>
</submission0845December162004>

Your </news> tag should be located at the end of the XML file, but that's not the case... Check it out... :)

3brand
12-16-2004, 09:14 PM
Yes, I realize that. But through the coding it doesn't work. The error I'm getting is that there can only be 1 parent category, in this case <news>. The problem is, I can't get the <news> part to appear just at the top and at the bottom, instead it appears before and after every <submissio></submission> The link I put up there has probably changed since I've been playing around with it.

CyanBlue
12-16-2004, 09:36 PM
Well... It looks like you are manipulating the XML structure in the ActionScript, right??? The code that you have above does not really tell me where you have <news> tag... Why not give us some more detail... :)

3brand
12-16-2004, 09:45 PM
I threw the <news> tags in there by hand. It doesn't it do it through the program, though I've tried, a lot. I need some way to put it in there ONLY at the TOP and the BOTTOM of the script and now have it repeat. XML requires 1 parent category and then everything else under it, right?

CyanBlue
12-16-2004, 10:10 PM
Well... Just add it in the beginning and the end of the string like this... It should work...

writeLVs.output = "<news>" + "<submission"+date_xml+"><title>"+title_cms+"</title><date>"+date_cms+"</date><content>"+content_cms+"</content></submission"+date_xml+">" + "</news>";

3brand
12-17-2004, 03:12 AM
The problem with that is that its within a on(release) button. So everytime you click on the button, the <news> thing is put in again.

The only way I can think of doing this is (somehow) with PHP take the contents of news.xml, put it in some variable, then put it between <news> and </news>. Having to do this everytime would be a pain in the ass. Plus I have no idea how to do this. Any ideas?

CyanBlue
12-17-2004, 11:03 AM
I don't quite understand the process, but are you saying that you are generating the XML file whenever the button gets pressed or something??? Is there any visual that I can see to understand the process??? Or, can you post a sample movie???

3brand
12-19-2004, 01:37 AM
Heres everything I have:

http://www.catwoodstudios.com/cms/cms.zip

Dark_Element
12-19-2004, 06:27 AM
....hum... reading through the code i can see that your a newbie to PHP... i think i found ur mistake... you need to change $output to $_POST['output'] because Flash is sending the values through the POST method...

3brand
12-19-2004, 08:59 PM
Yes, I'm a newbie to PHP, Flash, and everything else. I changed what you said to and it didn't fix my problem. Here is the PHP code:


<?php
$fp = fopen("news.xml", "a");
fputs($fp, $_POST['output']);
fclose($fp);
?>


Remember, I'm trying to get <news> and </news> around the WHOLE news.xml file. So it appears at the top and at the bottom, no where in between.

CyanBlue
12-19-2004, 09:30 PM
Ah... I think I now know what's going on...

You'll need to add the logic to add the <news> and </news> into the PHP file because you are keep appending the new nodes everytime you click on the submit button... :)

Read the content of the XML file first... Check to see if the first 6 characters are <news> and the last 7 characters are </news>... If they are not, you need to manually add those strings...

3brand
12-19-2004, 11:42 PM
I don't know how to :(

But I'm thinking it could work like this:

User puts data into the Flash CMS.
PHP file (flash_to_xml.php) grabs the data, clears the first 6 and last 7 characters, and puts the data into the XML file (news.xml) then finally puts in <news> and </news> at the beginning and end.

Alright, any ideas on how to code this because I have no idea how to use PHP.

Thanks! Oh, do you think it would be possible for me to give this Flash CMS out? Like distribute it to people like me, so they could use it? Does it even have any use outside of my situation?

CyanBlue
12-20-2004, 10:59 AM
Well... That'd be pretty much the same thing you will do in Flash... and I don't have time to look up the PHP manual myself because I don't do well in PHP either... But this is the process if you are interested doing it yourself... (Most of the stuff is in the PHP manual, so look it up...)

Have PHP load the content of the XML file by using fgets() function...
Once you have the content of the XML file stored into a variable, use substr() function to check if the first 6 characters are <news> and if it is not, add it in front of the content...
Check the last 7 characters to see if it is </news> and if it is, erase it from the content...
Add the output from the Flash at the bottom of the content...
and add the </news> at the end and write the file back...

3brand
12-20-2004, 04:23 PM
Thanks a bunch!

3brand
12-22-2004, 03:54 AM
Alright, I've been working on the PHP and this is what I have so far:


<?php
$fp = fopen("news.xml", "w");
$filename = 'news.xml';
$length = strlen($fp);
$op_news_chk = substr($fp, 5);
$cl_news_chk = substr($fp, -7, -1);
if($op_news_chk=="<news>"||"<News>"||"<NEWS>")
{
} else { fwrite($filename, '<news>');
}
if($cl_news_chk=="</news>"||"</News>"||"</NEWS")
{ substr_replace($fp, '', -7, -1);
}
fwrite($fp, $_POST['output']);
fwrite($filename, $fp);
fwrite($filename, '</news>');
fclose($fp);
?>


Here is the contents from news.xml:
<submission1525December212004>
<title>Test4</title>
<date>15:26 - Tuesday, December 21, 2004</date>
<content>Test4</content>
</submission1525December212004>

This means that its not putting in the <news> and the </news>. I thought it may have something to do with:

$fp = fopen("news.xml", "w");

With the "w" and that maybe it should be "w+"...GAH! I'm so lost. Please help?

CyanBlue
12-22-2004, 10:01 PM
Something along this sort of approach might work...

<?php
$filename = 'news.xml';

$fp = fopen($filename, "r");
$str = "";
while (!feof($fp))
{
$str = $str . fgets($fp, 4096);
}
fclose($fp);

$op_news_chk = strtoupper(substr($str, 0, 6));
$cl_news_chk = strtoupper(substr($str, -7, 7));

if ($op_news_chk == "<NEWS>")
{
$str = substr($str, 6);
}

if ($cl_news_chk == "</NEWS>")
{
$str = substr($str, 0, count($str) - 8);
}

$content = "<NEWS>" . $str . "<BR>" . $_GET['output'] . "</NEWS>";

$fp = fopen($filename, "w+");
fwrite($fp, $content);
fclose($fp);
?>

3brand
12-22-2004, 10:36 PM
Thanks for trying to help CyanBlue. Unfortunately that script doesn't work. All that it outputs is:

<NEWS><BR></NEWS>

When I changed the news.xml file to:

<news></news>

And ran the script again it changed it to:

<BR></NEWS>

The good news is that I don't think Flash requires a parent tag. I've been playing around with it and have successfully retrieved information without having a parent tag.

CyanBlue
12-22-2004, 10:49 PM
When I try with your XML content, it came out alright... So... Fiddle with the script... You should be able to make proper modification... :)

What's the parent tag??? I am not sure what you are talking about...

3brand
12-23-2004, 06:45 PM
Well, whenever I would look at the XML file through Firefox it would display errors and such because there was no parent tag. The parent tag in my case is the <news> and </news> tag. Apparently for XML to be error free you need something that encompassses everything in the XML document.

Lets say this is my XML data:

<submission1>
<title>Title 1</title>
<date>December 23</date>
<content>Here is my content.</content>
</submission1>
<submission2>
<title>Title 2</title>
<date>December 22</date>
<content>Here is my earlier content.</content>
</submission2>


The above XML document has errors because there is no PARENT tag, like <news>. I thought that Flash would not be able to read the data within it because of this fact, but I was wrong. Flash has no problems reading the data. For example:

trace(xml_doc.firstChild.firstChild.firstChild);

Would output: Title 1

trace(xml_doc.firstChild.nextSibling.firstChild.fi rstChild);

Would output: Title 2

So I guess all this extra PHP stuff was pointless. When I'm finished with this CMS stuff, should I upload it so everyone can use it?

CyanBlue
12-23-2004, 06:49 PM
That's a very very very big DOH!!! :D
But I suggest you add the <news> </news> tag just in case somebody else want to load that XML into another application... (You never know, right???)

Yeah... When you are done with all this, create little tutorial and post it here... I am sure there are people who will get benefitted from it... :)

Where in Chicago??? I used to live like 5 minutes from the UIC... :)

3brand
12-23-2004, 08:10 PM
Heh, I'm northwest side. You used to be a Hyde Parker, eh? Small world :)

CyanBlue
12-23-2004, 11:30 PM
Yes, in deed... :)

rune_star
12-29-2004, 09:08 AM
Merry x-mas all!

I'm looking very much forward to see the outcome of this project...Could be a nice christmas/new year present for all of us not-able-to-code flash designers...

tsnipert
04-06-2005, 08:33 PM
Through these articles I understand how to write to xml from flash great work! The only question I have now is how do you write a " to xml what would the syntax be?

My preffered outcome will look like this..
<image title="Balanced Breakfast" main="images/pic_1.jpg" />
but at the moment this is what I recieve as output...
<image title=Balanced Breakfast main=images/pic_1.jpg />

So if anyone knows the syntax to write/send " 's to a file it would be great

Thanks

mmm..pi..3.14..
04-06-2005, 09:27 PM
there is a single quote and a double quote, wrap single quotes around a double quote, or vice-versa. The other option is to use the backslash.

var mystring = '"';
trace(mystring); //outputs "

or

var mystring = "\\"";
trace(mystring); //outputs "

Eric :)