Results 1 to 3 of 3

Thread: writing to xml using php

  1. #1
    Join Date
    Sep 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default writing to xml using php

    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):
    Code:
    <?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>

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    <?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).

    Code:
    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;
    }
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I'm going to close this thread and redirect any followup to this thread--
    http://www.dynamicdrive.com/forums/s...ad.php?t=14165
    It's on the same subject and has more info, currently.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •