PDA

View Full Version : Control Growing Files


largo
03-07-2008, 10:16 AM
Hi, here is my situation. I have an online flash game that tracks some data from the users.
Here's how it works: Flash sends data to PHP and PHP writes that data to an XML file.
Now what would be the best way to make sure that the XML's filesize doesn't grow too big? :confused:
Can I add some code to the PHP that checks the XML's filesize and creates a new XML file if it gets too big?
I don't know PHP that well so I need some help. I know that I could just do this by hand regularly on the server, but I want to know if there is some way to automate this process, so if there is any other way please let me know. :)

This is my simple PHP code, that writes the flash data to PHP:
<?php
$filename = "xmldata/trackingData.xml";
$raw_xml = file_get_contents("php://input");

print $raw_xml;

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

Cota
03-08-2008, 04:34 AM
Something like this, obviously you could/should expand on this

<?php
$filename = "xmldata/trackingData.xml";
$raw_xml = file_get_contents("php://input");

print $raw_xml;


//Check File Size. The size returned is in BYTES
$size = filesize($filename);

//If XML file is larger than 50KB's then make a copy.
if($size > 51200){
//Copy file with new name containing the day it was copied
$today = date("Ymd");
$newFile = "xmldata/trackingData".$today.".xml"
copy($filename, $newFile);

//Overwrite file with new data.
$fp = fopen($filename, "w");
fwrite($fp, $raw_xml);
fclose($fp);
} else {
//Write normally
$fp = fopen($filename, "a");
fwrite($fp, $raw_xml);
fclose($fp);
}
?>

largo
03-11-2008, 11:08 AM
Thanks for your reply Cota:), but Im still having a problem, I need
this code: "<?xml version="1.0"?><Tracks></Tracks> " in the XML so that flash can write in it. So could I write this code as a PHP string into the XML-file when the "Overwrite file with new data" script is running?

<?php
$filename = "xmldata/trackingData.xml";
$raw_xml = file_get_contents("php://input");

print $raw_xml;

//Check File Size. The size returned is in BYTES
$size = filesize($filename);

//If XML file is larger than 50KB's then make a copy.
if($size > 51200){

//Copy file with new name containing the day it was copied
$today = date("Ymd");
$newFile = "xmldata/trackingData".$today.".xml";
copy($filename, $newFile);

//Overwrite file with new data. -----------------------------!!!!!!!!!!!!!!!!
$fp = fopen($filename, "w");
fwrite($fp, $raw_xml); //How do I put the clean XML here?
fclose($fp);


} else {
//Write normally
$fp = fopen($filename, "w");
fwrite($fp, $raw_xml);
fclose($fp);
}
?>

Cota
03-11-2008, 02:09 PM
You should be able to if you treat the XML file as text

largo
03-11-2008, 02:33 PM
I figured it out, the trick was to put the XML variable in '' quotes.

So now the code looks like this:
<?php
$filename = "xmldata/trackingData.xml";
$raw_xml = file_get_contents("php://input");
$clean_xml = '<?xml version="1.0"?><Tracks></Tracks>';

print $raw_xml;

//Check File Size. The size returned is in BYTES:-------------
$size = filesize($filename);

//If XML file is larger than 50KB's then make a copy.
if($size > 51200){

//Write normally the last entry:---------------------------
$fp = fopen($filename, "w");
fwrite($fp, $raw_xml);
fclose($fp);

//Copy file with new name containing the day it was copied
$today = date("Ymd");
$newFile = "xmldata/trackingData".$today.".xml";
copy($filename, $newFile);

//Overwrite file to clean XML------------------------------
$fp = fopen($filename, "w");
fwrite($fp, $clean_xml);
fclose($fp);

} else {
//Write normally:------------------------------------------
$fp = fopen($filename, "w");
fwrite($fp, $raw_xml);
fclose($fp);
}
?>

Thanks again for your help Cota:)

Cota
03-11-2008, 08:11 PM
Glad we could help.