Page 1 of 1

Using PHP to create XML folder structure

Posted: Sat Nov 15, 2008 8:28 pm
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

Re: Using PHP to create XML folder structure

Posted: Sat Nov 15, 2008 9:05 pm
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...

Re: Using PHP to create XML folder structure

Posted: Sat Nov 15, 2008 9:12 pm
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.

Re: Using PHP to create XML folder structure

Posted: Sat Nov 15, 2008 9:34 pm
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.

Re: Using PHP to create XML folder structure

Posted: Sat Nov 15, 2008 9:48 pm
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.

Re: Using PHP to create XML folder structure

Posted: Sat Nov 15, 2008 10:22 pm
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");

Re: Using PHP to create XML folder structure

Posted: Sun Nov 16, 2008 9:15 am
by cessnajumpin
Great!

Thanks a ton, I will play with that today!