This code
- traverses a directory path recursively and build an array from the list of files.
- creates XML data from that list of files
- saves it to XML file
DirectoryToXml.class.php
<?php
/**
* DirectoryToXml Class
*
* @PHPVER : 5.0
* @author : MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
* Moderator, phpResource (http://groups.yahoo.com/group/phpresource/)
* URL: http://www.rupom.info
* @version : 1.0
* @date : 07/26/2006
* Purpose : Creating XML File of a Given Directory Listing
*/
include_once("class.array2xml2array.php");
class DirectoryToXml extends CArray2xml2array
{
public $arr;
private $mainString;
private $path;
/**
* Constructor
* @access public
* @param $path
*/
public function __construct($path)
{
$this->arr = array();
$this->mainString = "";
$this->path = $path;
}
/**
* Traverses Files in Directory
* @param $path to traverse
* @return none
*/
public function traverseDirFiles($path)
{
$dir = opendir ($path);
while ($file = readdir ($dir))
{
if (($file == ".") or ($file == ".."))
{
continue;
}
if (filetype("$path/$file") == "dir")
{
$this->traverseDirFiles("$path/$file");
}
else
{
$this->processFiles($path,$file);
}
} //End of while
closedir($dir);
}//End of function
/**
* Put $file to path array
* @param $path, $file
* @return none
*/
function processFiles($path, $file)
{
//assigns $file to array "$arr"
eval("\$$vr=\"$file\";");
}//EOFn
/**
* Shows files and directories in a hierarchical structure from the given directory
* @return none
*/
public function showDirHierarchy()
{
$this->dBug($this->arr);
}
/**
* For Debuging/displaying purpose
* @param data to debug
* @return none
**/
function dBug($dump)
{
echo "<pre>";
print_r($dump);
echo "</pre>";
}
/**
* Forms path array from path string
* @param path string
* @return path array
**/
function formArray($str)
{
$arr = explode("$this->path/",$str);
$arr = explode("/",$arr[1]);
foreach($arr as $i=>$v)
{
$arr_to_form .= "[$v]";
}
return $arr_to_form;
}
}//EO Class
?>
Usage Exmaple
<?php
/**
* Usage of DirectoryToXml Class
*
* @PHPVER : 5.0
* @author : MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
* Moderator, phpResource (http://groups.yahoo.com/group/phpresource/)
* URL: http://www.rupom.info
* @version : 1.0
* @date : 07/26/2006
* Purpose : Creating XML File of a Given Directory Listing
*/
include("DirectoryToXml.class.php");
//set path you want to get XML for
$path = "/home/projects/rupom/uploads/auction/httpdocs/admin"; //change it according to your need
//Path to Array
$Obj = new DirectoryToXml($path);
$Obj->traverseDirFiles($path);
$Obj->showDirHierarchy();
//Array to XML
$Obj->setArray($Obj->arr);
$xml_file = "test.xml";
if ($Obj->saveArray($xml_file))
{
echo "<p><b>Array Converted To XML And Saved As $xml_file </b></p>";
}