Log in

View Full Version : writing to xml using php



chrishaswhiskey
09-16-2007, 10:29 PM
hi every one, i no this is probably really cheeky of me to ask this in my first post but hopefully ya can help,

so i have this .swf file whihc loads a picture from a folder using an external xml file whihc is fine, but i wanted it so i can make a page on my website and have a file upload thing on it and a user selects the file from there computer and uploads it to the images directory and also wrights the image detals and loctaion in the xml file so that the picture will load into the swf file without me or anyone else having to manually do it,

i dont no if its possible but if anyone can help i would be extreamly grateful,

Chris

This is the xml file (called photoflow.xml):

<?xml version="1.0" encoding="utf-8"?>
<photos path="images/">
<photo name="Photo 1" url="1.jpg">This is the optional description for photo 1</photo>
<photo name="Photo 2" url="2.jpg">This is the optional description for photo 2</photo>
<photo name="Photo 3" url="3.jpg">This is the optional description for photo 3</photo>
<photo name="Photo 4" url="4.jpg">This is the optional description for photo 4</photo>
<photo name="Photo 5" url="5.jpg">This is the optional description for photo 5</photo>
</photos>

djr33
09-17-2007, 12:54 AM
<?php
$f = fopen($imgname.'.xml','w+');
fwrite($f,$xmlhere);
fclose ($f);
?>

Also, there are some xml functions in php to read/write, but I find them to be quite difficult to use.

Here are some that I wrote that will generate an array from xml (no attributes will be included, just the content of each tag and the tag names-- though this could be added as a function, though i'm not sure how it would be best stored as a value in the array).


function checkxmlfortags($xml) {
if(strpos($xml,'</'.substr($xml,strpos($xml,'<')+1,(strpos($xml,'>',strpos($xml,'<'))-strpos($xml,'<'))-1).'>',strpos($xml,'<',strpos($xml,'<')))) {
return TRUE;
}
}
function xmltoarray($xml) {
while (strpos($xml,'<?xml')<strpos($xml,'?>')) $xml = substr($xml,0,strpos($xml,'<?xml')).substr($xml,strpos($xml,'?>',strpos($xml,'<?xml')));
while (strpos($xml,'<!--')<strpos($xml,'-->')) $xml = substr($xml,0,strpos($xml,'<!--')).substr($xml,strpos($xml,'-->',strpos($xml,'<!--')));
while(checkxmlfortags($xml)) {
list($xmla,$xmlb) = explode('<',$xml,2);
list($xmltag,$xmlb) = explode('>',$xmlb,2);
$xmltag = urldecode($xmltag);
if (strpos($xmltag,' ')) {
list($xmltag,$xmlparams) = explode(' ',$xmltag,2);
}
$n=strlen($xmlb);
for ($i=$n;$i>0;$i--) {
if (substr_count(substr($xmlb,0,$i),'</'.$xmltag.'>')-substr_count(substr($xmlb,0,$i),'<'.$xmltag.'>')==1) {
$n=$i;
}
}
$xmldata = trim(substr($xmlb,0,$n-(strlen($xmltag)+3)));
$xmlb = substr($xmlb,$n);
if (isset($xmlarray[$xmltag])) {for ($n=0;isset($xmlarray[$xmltag.'_'.$n]);$n++) {} $xmltag.='_'.$n;}
$xmlarray[$xmltag] = checkxmlfortags($xmldata) ? xmltoarray($xmldata) : urldecode($xmldata);
$xml = $xmlb;
}
return isset($xmlarray) ? $xmlarray : array();
}
function arraytoxml($array,$spacer=' ',$level=0) {
$spacing='';
for($i=$level;$i>0;$i--) { $spacing.=$spacer; }
$xml='';
foreach($array as $tag=>$val) {
$xml .= $spacing.'<'.urlencode($tag).">\n";
if (is_array($val)) {
$xml .= arraytoxml($val,$level+1);
}
else if ($val!='') {
$xml .= $spacing.urlencode($val)."\n";
}
$xml .= $spacing.'</'.urlencode($tag).">\n";
}
return ($level==0)?trim($xml):$xml;
}

djr33
09-17-2007, 02:55 PM
I'm going to close this thread and redirect any followup to this thread--
http://www.dynamicdrive.com/forums/showthread.php?t=14165
It's on the same subject and has more info, currently.