Using PHP to create XML folder structure

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cessnajumpin
Forum Newbie
Posts: 4
Joined: Sat Nov 15, 2008 8:15 pm

Using PHP to create XML folder structure

Post by cessnajumpin »

Hi guys,

So I'm not exactly a Noob at this, but I've been away from anything really in depth with PHP for over a year now.

The problem I have is that I have a client who wants to be able to put a folder of images on her server and go to a page where the PHP will have written out an XML syntaxed page. It would be easier for her to just be able to copy and paste from the window, as opposed to me setting up an xml_string function.

I've got something pretty close set up at:
http://www.jmelnick.com/temp/KF/images/HMxml/maphp/

I started w/ dospeixos.net maphp script, and I've gotten it to where it will list out the individual images in the correct style, but not the folder path to get to the images. However, I can easily get the path itself to echo, just having trouble splicing it. Does anybody have any suggestions, or other ways to do this?

Any help would be greatly appreciated.

Thanks,
Jack
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using PHP to create XML folder structure

Post by requinix »

What should the resulting XML look like? You could put each <image> inside a <path> inside another <path>, or the path could be an attribute of an image, or...
cessnajumpin
Forum Newbie
Posts: 4
Joined: Sat Nov 15, 2008 8:15 pm

Re: Using PHP to create XML folder structure

Post by cessnajumpin »

Hey tasairis, ideally the end result would be

<node1 attribute = "Female">
<node2 attribute = "Size1">
<node3 attribute = "TheirName">
<image>123.jpg</image>
<image>123.jpg</image>
<image>123.jpg</image>
</node3>
</node2>
</node1>

Currently, I'm not really worried about getting the xml to write out correctly with the nodes in all the right places.

I'm just trying to figure out a way to isolate each folder of the path that is being printed out.

I hope that makes sense.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using PHP to create XML folder structure

Post by requinix »

Your example was doing something recursively. Depth-first if I'm not mistaken.
The key is to keep track of each path component between function calls rather than lump them all together.

For example, use an array. Push each new directory onto the end of it when calling, and use implode to turn it into a path.
cessnajumpin
Forum Newbie
Posts: 4
Joined: Sat Nov 15, 2008 8:15 pm

Re: Using PHP to create XML folder structure

Post by cessnajumpin »

Hmm....

Yeah, I guess that's one of the downfalls about trying to repurpose code.

Is there an implode that doesn't need the first argument though? I've updated the page and the path is getting pushed into
an array. But it doesn't appear to be separated by a comma, and when I try to use that as an argument it throws an error.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using PHP to create XML folder structure

Post by requinix »

Okay, didn't quite describe it correctly. And I gave you something more complicated than it needed to be.

Code: Select all

function listImages($dir) {
    $h = opendir($dir);
    while ($i = readdir($h)) {
        if ($i == "." || $i == "..") continue;
        $j = $dir . "/" . $i;
        if (is_dir($j) {
            echo "<node attribute=\"$i\">\n";
            listImages($j);
            echo "</node>\n";
        } else if (getimagesize($j)) {
            echo "<image>$i</image>\n";
        }
    }
}
listImages("/path/to/images");
cessnajumpin
Forum Newbie
Posts: 4
Joined: Sat Nov 15, 2008 8:15 pm

Re: Using PHP to create XML folder structure

Post by cessnajumpin »

Great!

Thanks a ton, I will play with that today!
Post Reply