Jump to content

php file erasing contents of xml file


aptprone

Recommended Posts

I have a php file that is supposed to amend an xml file based on a flash movie. The problem is, it is totally erasing the contents of the file. Here is the php code:

 

<?php
$myFile = $_POST['pathXML'];
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_POST['newXML'];

$newStringData =  stripslashes($stringData);
$newStringData = htmlspecialchars_decode($newStringData);
fwrite($fh, $newStringData);

fclose($fh);
?>

 

Does anyone see any apparent problems with this??? I'm not a php guy. If the problem isn't there, it might be in the flash action script which is as follows:

 

import caurina.transitions.Tweener;
import caurina.transitions.properties.ColorShortcuts;
ColorShortcuts.init();

//load xml
var xml:XML = new XML();
xml.ignoreWhite = true;
_root.xmlPath == undefined ? xml.load("settings.xml?"+Math.round(Math.random()*500)) : xml.load(_root.xmlPath+"?"+Math.round(Math.random()*500));

var i:Number;
var this2:Object = this;
var cookie:SharedObject = SharedObject.getLocal("poll");
var noVote:Boolean = false;
xml.onLoad = function(succes:Boolean) {
if (success) {

	i = Number(xml.firstChild.childNodes[1].attributes.no)-1;
	//check if user has already voted in the past
	if (xml.firstChild.childNodes[0].attributes.restrictMultiple == "true") {
		if ((cookie.data.voted != String(xml.firstChild.childNodes[1].childNodes[i].attributes.question).slice(0, 5) || cookie.data.voted == undefined)) {
			noVote = true;
		}
	} else {
		noVote = true;
		cookie.clear();
	}

	//write main question
	mcText.autoSize = true;
	mcText.text = String(xml.firstChild.childNodes[1].childNodes[i].attributes.question);
	mcText._width = Number(xml.firstChild.childNodes[0].attributes.width);
	(new Color(mcText).setRGB(Number(xml.firstChild.childNodes[0].attributes.text)));

	//create radio group
	mcHolder.attachMovie("radio","mcItem"+i,mcHolder.getNextHighestDepth());
	mcHolder["mcItem"+i]._y = mcText._height+20;
	mcHolder["mcItem"+i]._x = 20;

	//remember the colors
	mcHolder["mcItem"+i].background = Number(xml.firstChild.childNodes[0].attributes.background);
	mcHolder["mcItem"+i].border = Number(xml.firstChild.childNodes[0].attributes.border);
	mcHolder["mcItem"+i].text = Number(xml.firstChild.childNodes[0].attributes.text);
	mcHolder["mcItem"+i].symbol = Number(xml.firstChild.childNodes[0].attributes.symbols);
	mcHolder["mcItem"+i].flash = Number(xml.firstChild.childNodes[0].attributes.invalid);
	mcHolder["mcItem"+i].backgroundRoll = Number(xml.firstChild.childNodes[0].attributes.backgroundRoll);
	mcHolder["mcItem"+i].borderRoll = Number(xml.firstChild.childNodes[0].attributes.borderRoll);
	mcHolder["mcItem"+i].textRoll = Number(xml.firstChild.childNodes[0].attributes.textRoll);
	mcHolder["mcItem"+i].backgroundSelect = Number(xml.firstChild.childNodes[0].attributes.backgroundSelect);
	mcHolder["mcItem"+i].borderSelect = Number(xml.firstChild.childNodes[0].attributes.border);
	mcHolder["mcItem"+i].textSelect = Number(xml.firstChild.childNodes[0].attributes.textSelect);

	//add items to the design
	var newYY:Number = 0;
	for (var j:Number = 0; j<xml.firstChild.childNodes[1].childNodes[i].childNodes.length; j++) {
		mcHolder["mcItem"+i].mcMenu.attachMovie("radio2","mcButton"+j,mcHolder["mcItem"+i].mcMenu.getNextHighestDepth());

		mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBord._width = 15;
		mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBord._height = 15;
		mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBack._width = 13;
		mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBack._height = 13;
		mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcSymbol._width = mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcSymbol._height=mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBack._width-6;
		mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcSymbol._y = (mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBack._height-mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcSymbol._height)/2+1;
		mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcSymbol._x = (mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBack._width-mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcSymbol._width)/2+1;

		(new Color(mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBack).setRGB(mcHolder["mcItem"+i].background));
		(new Color(mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcSymbol).setRGB(mcHolder["mcItem"+i].symbol));
		(new Color(mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBord).setRGB(mcHolder["mcItem"+i].border));
		(new Color(mcHolder["mcItem"+i].mcMenu["mcButton"+j].txtLabel).setRGB(mcHolder["mcItem"+i].text));

		mcHolder["mcItem"+i].mcMenu["mcButton"+j].txtLabel.autoSize = true;
		mcHolder["mcItem"+i].mcMenu["mcButton"+j].txtLabel.text = String(xml.firstChild.childNodes[1].childNodes[i].childNodes[j].attributes.answer);
		mcHolder["mcItem"+i].mcMenu["mcButton"+j].txtLabel._width = Number(xml.firstChild.childNodes[0].attributes.width)-mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBord._width-30;

		mcHolder["mcItem"+i].mcMenu["mcButton"+j]._y = newYY;
		newYY += mcHolder["mcItem"+i].mcMenu["mcButton"+j].txtLabel._height;
	}
	mcHolder["mcItem"+i].mcBord._height = mcHolder["mcItem"+i].mcMenu._height-5;

	//add button
	mcHolder.attachMovie("button","mcButton",mcHolder.getNextHighestDepth());
	mcHolder.mcButton.mcLabel.txtLabel.text = String(xml.firstChild.childNodes[0].attributes.buttonLabel);

	mcHolder.mcButton.mcBack._width = Number(xml.firstChild.childNodes[0].attributes.buttonWidth);
	mcHolder.mcButton.mcBack._height = Number(xml.firstChild.childNodes[0].attributes.buttonHeight);
	mcHolder.mcButton.mcBord._width = Number(xml.firstChild.childNodes[0].attributes.buttonWidth)+2;
	mcHolder.mcButton.mcBord._height = Number(xml.firstChild.childNodes[0].attributes.buttonHeight)+2;
	mcHolder.mcButton.mcLabel.txtLabel._width = Number(xml.firstChild.childNodes[0].attributes.buttonWidth)-20;

	mcHolder.mcButton.background = Number(xml.firstChild.childNodes[0].attributes.background);
	mcHolder.mcButton.border = Number(xml.firstChild.childNodes[0].attributes.border);
	mcHolder.mcButton.text = Number(xml.firstChild.childNodes[0].attributes.text);
	mcHolder.mcButton.backgroundRoll = Number(xml.firstChild.childNodes[0].attributes.backgroundRoll);
	mcHolder.mcButton.borderRoll = Number(xml.firstChild.childNodes[0].attributes.borderRoll);
	mcHolder.mcButton.textRoll = Number(xml.firstChild.childNodes[0].attributes.textRoll);

	(new Color(mcHolder.mcButton.mcBack).setRGB(mcHolder.mcButton.background));
	(new Color(mcHolder.mcButton.mcBord).setRGB(mcHolder.mcButton.border));
	(new Color(mcHolder.mcButton.txtLabel).setRGB(mcHolder.mcButton.text));

	mcHolder.mcButton._y = mcHolder._height+15;
	mcHolder.mcButton._x = Number(xml.firstChild.childNodes[0].attributes.width)-mcHolder.mcButton._width-30;

	//position this
	mcHolder["mcItem"+i]._y = newY;
	if (xml.firstChild.childNodes[1].childNodes[i].childNodes[i].attributes.extraMargin != undefined) {
		mcHolder["mcItem"+i]._y += Number(xml.firstChild.childNodes[1].childNodes[i].childNodes[i].attributes.extraMargin);
		newY += Number(xml.firstChild.childNodes[1].childNodes[i].childNodes[i].attributes.extraMargin);
	}
	newY += mcHolder["mcItem"+i].mcBord._height+Number(xml.firstChild.childNodes[0].attributes.margin);
	mcHolder["mcItem"+i].mcInput.txtText.tabIndex = i;

	mcHolder["mcItem"+i].tabIndex = 0;
	mcHolder["mcItem"+i].tabEnabled = false;

	if (!noVote) {
		displayResults();//display results(updated)
	}
}
};

function sendVote() {
//if user has selected something
if (selectedObj != undefined) {
	if (xml.firstChild.childNodes[0].attributes.restrictMultiple == "true") {
		cookie.data.voted = String(xml.firstChild.childNodes[1].childNodes[i].attributes.question).slice(0, 5);
	} else {
		cookie.clear();
	}
	displayResults();//display results(updated)
	createNewXML();//create new xml file
	saveNewXML();//save the new xml file
} else {
	var j:Number = 0;
	while (mcHolder["mcItem"+i].mcMenu["mcButton"+j] != undefined) {
		mcHolder["mcItem"+i].mcMenu["mcButton"+j].flashError(mcHolder["mcItem"+i].mcMenu["mcButton"+j]);
		j++;
	}
}
}

//function that displays poll results
function displayResults() {
var newYY:Number = 0;
var totalVotes:Number = 1;
if (!noVote && xml.firstChild.childNodes[0].attributes.restrictMultiple == "true") {
	totalVotes--;
}
for (var j:Number = 0; j<xml.firstChild.childNodes[1].childNodes[i].childNodes.length; j++) {
	totalVotes += Number(xml.firstChild.childNodes[1].childNodes[i].childNodes[j].attributes.votes);
}
for (var j:Number = 0; j<xml.firstChild.childNodes[1].childNodes[i].childNodes.length; j++) {

	//move and hide items
	Tweener.addTween([mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBack, mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBord, mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcSymbol],{_alpha:0, _x:-100, time:.5});
	Tweener.addTween(mcHolder["mcItem"+i].mcMenu["mcButton"+j].txtLabel,{_y:mcHolder["mcItem"+i].mcMenu["mcButton"+j]._y+newYY, _x:mcHolder["mcItem"+i].mcMenu["mcButton"+j].txtLabel._x-mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBord._width-5, time:.5});
	newYY += 5;

	//reset chart
	mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBars._y = mcHolder["mcItem"+i].mcMenu["mcButton"+j]._y+newYY+15;
	mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBars.mcTotal._width = mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBars.mcVoted._width=0;
	(new Color(mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBars.mcTotal).setRGB(Number(xml.firstChild.childNodes[0].attributes.totalColor)));
	(new Color(mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBars.mcVoted).setRGB(Number(xml.firstChild.childNodes[0].attributes.votedColor)));

	//calculate votes procent
	var votesProcent:Number;
	if (selectedObj == mcHolder["mcItem"+i].mcMenu["mcButton"+j]) {
		if (noVote) {
			votesProcent = Math.round(100*(Number(xml.firstChild.childNodes[1].childNodes[i].childNodes[j].attributes.votes)+1)/totalVotes);
		} else {
			votesProcent = Math.round(100*Number(xml.firstChild.childNodes[1].childNodes[i].childNodes[j].attributes.votes)/totalVotes);
		}
	} else {
		votesProcent = Math.round(100*Number(xml.firstChild.childNodes[1].childNodes[i].childNodes[j].attributes.votes)/totalVotes);
	}

	//update text
	mcHolder["mcItem"+i].mcMenu["mcButton"+j].txtLabel._width += mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBord._width+5;
	if (xml.firstChild.childNodes[0].attributes.displayPercent == "true") {
		setTimeout(updateText,500,votesProcent,mcHolder["mcItem"+i].mcMenu["mcButton"+j].txtLabel,1);
	} else {
		if (selectedObj != mcHolder["mcItem"+i].mcMenu["mcButton"+j]) {
			setTimeout(updateText,500,Number(xml.firstChild.childNodes[1].childNodes[i].childNodes[j].attributes.votes),mcHolder["mcItem"+i].mcMenu["mcButton"+j].txtLabel,2);
		} else {
			setTimeout(updateText,500,Number(xml.firstChild.childNodes[1].childNodes[i].childNodes[j].attributes.votes)+1,mcHolder["mcItem"+i].mcMenu["mcButton"+j].txtLabel,2);
		}
	}

	//animate chart   
	Tweener.addTween(mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBars.mcTotal,{_width:mcHolder["mcItem"+i].mcMenu["mcButton"+j]._width, _alpha:100, time:.5, delay:.5});
	Tweener.addTween(mcHolder["mcItem"+i].mcMenu["mcButton"+j].mcBars.mcVoted,{_width:votesProcent*mcHolder["mcItem"+i].mcMenu["mcButton"+j]._width/100, _alpha:100, time:.5, delay:.9});

}
if (xml.firstChild.childNodes[0].attributes.displayTotal == "true") {
	setTimeout(totalText,800,totalVotes,totalHeight);
}
Tweener.addTween(mcHolder.mcButton,{_alpha:0, time:.1});
}
//function that updates the label text with the percent or number of votes
function updateText(what:Number, where:TextField, type:Number) {
if (type == 1) {
	where.text += " ("+String(what)+"%)";
} else {
	where.text += " ("+String(what)+")";
}
}
//function that displays the total votes text if required
function totalText(votes:Number) {
txtTotal._alpha = 0;
txtTotal.text = String(xml.firstChild.childNodes[0].attributes.totalText)+String(votes);
txtTotal._width = mcHolder["mcItem"+i].mcMenu.mcButton0._width-60;
txtTotal.autoSize = true;
(new Color(txtTotal).setRGB(Number(xml.firstChild.childNodes[0].attributes.text)));
txtTotal._y = this2._height+15;
Tweener.addTween(txtTotal,{_alpha:100, time:.2});
}

//function that creates an xml string file with the poll results updated
var fileString:String = new String();
function createNewXML() {
fileString = '<?xml version="1.0" encoding="utf-8"?>\n<settings>\n\t';//header
fileString += String(xml.firstChild.firstChild)+'\n\n\t<polls no="'+(i+1)+'">\n';//design node
for (var k:Number = 0; k<xml.firstChild.childNodes[1].childNodes.length; k++) {
	if (k == i) {
		//changed poll node
		fileString += '\n\t\t'+String(xml.firstChild.childNodes[1].childNodes[k]).slice(0, String(xml.firstChild.childNodes[1].childNodes[k]).indexOf('">')+2);
		var no:Number = Number(selectedObj._name.slice(8, selectedObj._name.length));
		for (var j:Number = 0; j<xml.firstChild.childNodes[1].childNodes[k].childNodes.length; j++) {
			if (j == no) {
				//updated result
				var node:String = String(xml.firstChild.childNodes[1].childNodes[k].childNodes[j]);
				fileString += node.slice(0, node.indexOf('votes="'));
				fileString += 'votes="'+(Number(xml.firstChild.childNodes[1].childNodes[k].childNodes[j].attributes.votes)+1)+'" />';//new votes number
			} else {
				//normal other results
				fileString += String(xml.firstChild.childNodes[1].childNodes[k].childNodes[j]);
			}
		}
		fileString += '</poll>';
	} else {
		//unchanged poll nodes
		fileString += '\n\t\t'+String(xml.firstChild.childNodes[1].childNodes[k]);
	}
}
fileString += '\n\n\t</polls>\n</settings>';//footer
}

//function that saves the new xml file
var file:LoadVars = new LoadVars();
function saveNewXML() {
file.newXML = fileString;
_root.xmlPath == undefined ? file.pathXML="settings.xml" : file.pathXML=String(_root.xmlPath);
file.sendAndLoad("poll.php",file,"POST");
}

var selectedObj:Object;
function hideAll() {
mcHolder._visible = false;
}

Stage.scaleMode = "noScale";
Stage.showMenu = false;

//position
this2._x = (10-Stage.width/2);
this2._y = (10-Stage.height/2);

 

anyone??? thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/167995-php-file-erasing-contents-of-xml-file/
Share on other sites

When you open a file with 'w', it's automatically "emptied" whether you write anything to it or not. You might want to consider opening it with 'r+', which leaves the content intact if you don't write to it.

 

Check the value of $_POST['newXML'] to ensure it contains the text to write.

 

Test that fwrite() has written data by using $bytesWritten = fwrite($fh, $newStringData); if $bytesWritten is a boolean false, or numeric 0, then nothing has been written to the file.

 

Consider writing to a second file, then moving it to overwrite the original if your write was successful.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.