Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.