PHP – File & Folder List into XML
- June 20th, 2011
- Posted in Uncategorized
So, after some searching around the web i couldn’t find a PHP function that would output a list of all folders and there files to XML. I needed this function for a current mobile app im working on for a client! (The app has around 60mb of images. Rather than having them download a 60mb binary from the appstore, it’ll download all images on the first run of the application! The XML structure tells the app what assets are available and where they can be found!) Anyway. Here’s the code! =]
<?php
header('Content-Type: text/xml');
print '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
function listContents($path = ".", $level = 0, $ignore = array("cgi-bin",".","..",".DS_Store")) {
$directory = @opendir($path);
while(false !== ($file = readdir($directory))){
if(!in_array($file,$ignore)){
if(is_dir("$path/$file")){
echo "<folder level='".$level."' name='".$file."'>";
listContents("$path/$file",($level+1)); echo "</folder>";
} else {
echo "<file>".$file."</file>";
}
}
}
closedir($directory);
}
echo "<filestucture>";
listContents(".");
echo "</filestucture>";
?>
Original code from missing-score via codingforums.com

No comments yet.