Jump to content

output dir to xml


tryingtolearn

Recommended Posts

I have a function which is listing my directories w/ files (Images) in them.

<?php
function sortDirectory($path = 'images') {  
  $ignore = array('.', '..', 'index.php');
  $folder = array();
  $dh = @opendir($path);
  while (false !== ( $file = readdir($dh) )) {
if (!in_array($file, $ignore)) {
  if (is_dir("$path/$file")) {
	$folder[$file] = sortDirectory("$path/$file");
  } else {
	$folder[$file] = "$path/$file";
  }
}
  }
  closedir($dh);
  ksort($folder);
  return $folder;
}

function getDirectory($folder = array(),$level = 0) {
// Set which extensions should be approved in the XML file
  $extensions = array(  'jpg', 'JPG',  'png', 'PNG',  'gif', 'GIF',  'bmp', 'BMP');
  $spaces = str_repeat(' ', ( $level * 4));
  foreach($folder as $file=>$path) {
if (is_array($path)) {
  echo "<strong>$spaces $file</strong><br />";
  getDirectory($path, $level+1);
}
else {
if ( in_array( substr($file, -3), $extensions ) ){
  echo "$spaces <a href=\"$path\">$file</a><br />";
  }
}
  }
}

getDirectory(sortDirectory());

?>

Im trying to get it to write an xml file in this fashion

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<dir1>
    <pic>
        <image>file1.jpg</image>        
    </pic>
    <pic>
        <image>file2.jpg</image>        
    </pic>    
</dir1>

<dir2>
    <pic>
        <image>file1.jpg</image>        
    </pic>
    <pic>
        <image>file2.jpg</image>        
    </pic>    
</dir2>

but I cannot seem to get a grasp on this simplexml for more than 1 directory - any help appreciated.?

Link to comment
https://forums.phpfreaks.com/topic/221609-output-dir-to-xml/
Share on other sites

Can't you just replace the html tags with xml tags?
Guess I could, figured there might be a better way to do it. The html tags were just to make it easier to make sure I was getting the results I wanted.

 

And do you need to save this xml to disk as an xml file?
Yes
Link to comment
https://forums.phpfreaks.com/topic/221609-output-dir-to-xml/#findComment-1147366
Share on other sites

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.