PHP - writing a directory into an XML file
I am having trouble writing my file directory of my website in to an XML file. I've searched all over google and have had no success with the examples that I've found. I've checked the php documentation but can't find anything really.
here's the code i've been working with:
PHP Code:
$contents = "<?xml version=\"1.0\" ?>\n";
$path = "easythanks/";
function outputPath($path){
$level=substr_count($path,'/');
for ($i=1;$i<$level;$i++) echo ' ';
echo basename($path);
echo "\n";
}
echo '<pre>';
traverseDirTree('./','outputpath','outputpath');
echo '</pre>';
function traverseDirTree($base,$fileFunc,$dirFunc=null,$afterDirFunc=null){
$subdirectories=opendir($base);
while (($subdirectory=readdir($subdirectories))!==false){
$path=$base.$subdirectory;
if (is_file($path)){
$contents .= "<item text=".$path." />";
if ($fileFunc!==null){
$fileFunc($path);
}
}else{
$contents .= "<item text=".$path.">";
if ($dirFunc!==null) $dirFunc($path);
if (($subdirectory!='.') && ($subdirectory!='..')){
traverseDirTree($path.'/',$fileFunc,$dirFunc,$afterDirFunc);
}
if ($afterDirFunc!==null) $afterDirFunc($path);
$contents .= "</item>";
}
}
}
$myFile = "test.xml";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $contents);
fclose($fh);
Can anyone please point me in the right direction? PS the xml file needs to look like this
Code:
<item text="directory1">
<item text="directory2">
<item text="child1" />
</item>
<item text="child2" />
<item text="child3"/>
</item>
Thanks,
David
Re: PHP - writing a directory into an XML file
I would normally do it via Dom
where is you root tag?
HTML Code:
<?xml version="1.0" ?>
<items>
<item text="directory1">
<item text="directory2">
<item text="child1" />
</item>
<item text="child2" />
<item text="child3"/>
</item>
</items>
PHP Code:
$filename = "test.xml";
$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->Load($filename);
$doc->formatOutput = true;
$items = $doc->createElement("items");
$doc->appendChild($items);
$path = "easythanks/";
function outputPath($path){
$level=substr_count($path,'/');
for ($i=1;$i<$level;$i++) echo ' ';
echo basename($path);
echo "\n";
}
echo '<pre>';
traverseDirTree($items,'./','outputpath','outputpath');
echo '</pre>';
function traverseDirTree($cNode, $base,$fileFunc,$dirFunc=null,$afterDirFunc=null){
$subdirectories=opendir($base);
while (($subdirectory=readdir($subdirectories))!==false){
$path=$base.$subdirectory;
if (is_file($path)){
$item = $doc->createElement("item");
$item->set_attribute('text', $path);
$cNode->appendChild($item);
if ($fileFunc!==null){
$fileFunc($path);
}
}else{
$item = $doc->createElement("item");
$item->set_attribute('text', $path);
$cNode->appendChild($item);
if ($dirFunc!==null) $dirFunc($path);
if (($subdirectory!='.') && ($subdirectory!='..')){
traverseDirTree($cNode,$path.'/',$fileFunc,$dirFunc,$afterDirFunc);
}
if ($afterDirFunc!==null) $afterDirFunc($path);
}
}
}
$doc->save($filename);
Note untested code