Jump to content

file structure into XML?


nikefido

Recommended Posts

Has anyone come across some info or know of a decent way to show a file structure in XML?

 

Right now my php creates a multi-dimensional array. If the main array's element is an array, it's a folder. If the element is not an array, it's a file.

 

My main purpose is to read through a file directory and create an XML file that can be loaded into a Flash movie to be read through (using AS3) to recreate the file structure in a flash movie.

 

So, the array as it is after my function that reads the directory:

Array
(
    [0] => /Users/Chris/Sites/_resources/.DS_Store
    [1] => Array
        (
            [0] => /Users/Chris/Sites/_resources/_images/.DS_Store
            [1] => /Users/Chris/Sites/_resources/_images/image1.jpg
            [2] => /Users/Chris/Sites/_resources/_images/image2.jpg
            [3] => /Users/Chris/Sites/_resources/_images/image3.jpg
            [4] => /Users/Chris/Sites/_resources/_images/image4.jpg
            [5] => /Users/Chris/Sites/_resources/_images/image5.jpg
            [6] => /Users/Chris/Sites/_resources/_images/image6.jpg
            [7] => /Users/Chris/Sites/_resources/_images/image7.jpg
            [8] => /Users/Chris/Sites/_resources/_images/image8.jpg
            [9] => /Users/Chris/Sites/_resources/_images/image9.jpg
            [10] => /Users/Chris/Sites/_resources/_images/left.psd
            [11] => /Users/Chris/Sites/_resources/_images/middle.psd
            [12] => /Users/Chris/Sites/_resources/_images/right.psd
            [13] => /Users/Chris/Sites/_resources/_images/thumb1.jpg
            [14] => /Users/Chris/Sites/_resources/_images/thumb2.jpg
            [15] => /Users/Chris/Sites/_resources/_images/thumb3.jpg
            [16] => /Users/Chris/Sites/_resources/_images/thumb4.jpg
            [17] => /Users/Chris/Sites/_resources/_images/thumb5.jpg
            [18] => /Users/Chris/Sites/_resources/_images/thumb6.jpg
            [19] => /Users/Chris/Sites/_resources/_images/thumb7.jpg
            [20] => /Users/Chris/Sites/_resources/_images/thumb8.jpg
            [21] => /Users/Chris/Sites/_resources/_images/thumb9.jpg
        )

    [2] => Array
        (
            [0] => /Users/Chris/Sites/_resources/_packages/.DS_Store
            [1] => Array
                (
                    [0] => /Users/Chris/Sites/_resources/_packages/_gallery/.DS_Store
                    [1] => /Users/Chris/Sites/_resources/_packages/_gallery/AC_RunActiveContent.js
                    [2] => /Users/Chris/Sites/_resources/_packages/_gallery/gallery.as
                    [3] => /Users/Chris/Sites/_resources/_packages/_gallery/image.as
                    [4] => /Users/Chris/Sites/_resources/_packages/_gallery/thumbnail.as
                )

            [2] => Array
                (
                    [0] => /Users/Chris/Sites/_resources/_packages/_gallery2/gallery.as
                    [1] => /Users/Chris/Sites/_resources/_packages/_gallery2/image.as
                    [2] => /Users/Chris/Sites/_resources/_packages/_gallery2/thumbnail.as
                )

            [3] => Array
                (
                    [0] => /Users/Chris/Sites/_resources/_packages/_preloader/preloader.as
                )

        )

    [3] => Array
        (
            [0] => /Users/Chris/Sites/_resources/_xml/.DS_Store
            [1] => /Users/Chris/Sites/_resources/_xml/gallery.xml
        )

    [4] => /Users/Chris/Sites/_resources/AlphaMasking.zip
    [5] => /Users/Chris/Sites/_resources/ape_a045.zip
    [6] => /Users/Chris/Sites/_resources/BloodEffect.as
    [7] => /Users/Chris/Sites/_resources/dropMenu.ZIP
    [8] => /Users/Chris/Sites/_resources/Example.zip
)

 

 

now my function to "convert" it into xml:

<?php
function arrayToXml($myArray) {
$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"\n";
$output .= "<structure>\n";

foreach($myArray as $xmlFile) {
	if(is_array($xmlFile)) {
		$newXMLFile = $xmlFile;
		$output .= "<directory>\n";
		arrayToXml($newXMLFile); //reiteration function
		//echo "<p>Directory: " . $xmlFile . "</p>";
		$output .= "</directory>\n";
	} else {
		$output .= "<file>: " . $xmlFile . "</file>\n";
		//echo "File: " . $xmlFile . "</p>";
	}//end if
}//end foreach
$output .= "</structure>";
return htmlentities($output); //html entities only for testing purposes
}//end funcion
?>

 

This function outputs:

<?xml version="1.0" encoding="UTF-8"\?>
<structure>
<file>: /Users/Chris/Sites/_resources/.DS_Store</file>
<directory>
</directory>
<directory>
</directory>
<directory>
</directory>
<file>: /Users/Chris/Sites/_resources/AlphaMasking.zip</file>
<file>: /Users/Chris/Sites/_resources/ape_a045.zip</file>
<file>: /Users/Chris/Sites/_resources/BloodEffect.as</file>
<file>: /Users/Chris/Sites/_resources/dropMenu.ZIP</file>
<file>: /Users/Chris/Sites/_resources/Example.zip</file>
</structure>

 

This clearly isn't all the files! (there's no files within the directory tags).

 

If I change my foreach function to just echo out all files and directories, it does so properly (those parts are commented out above).

 

Why isn't this outputting correctly?

Link to comment
https://forums.phpfreaks.com/topic/86524-file-structure-into-xml/
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.