rondog Posted August 27, 2009 Share Posted August 27, 2009 I am generating XML with a recursive folder function. I am not able to close out my project node for some reason. <?php echo "<?xml version=\"1.0\" encoding=\"utf-8\"?> <structure>\n"; getDirectory("clients/xyzclient1"); function getDirectory($path = '.', $level = 0) { // Directories to ignore when listing output. Many hosts // will deny PHP access to the cgi-bin. $ignore = array('cgi-bin', '.', '..'); // Open the directory to the handle $dh $dh = @opendir($path); // Loop through the directory while (false !== ($file = readdir($dh))) { // Check that this file is not to be ignored if (!in_array($file, $ignore)) { // Just to add spacing to the list, to better // show the directory tree. $spaces = str_repeat(' ', ($level * 4 )); // Its a directory, so we need to keep reading down... if (is_dir("$path/$file")) { // Re-call this same function but on a new directory. // this is what makes function recursive. echo "\t<project name=\"$file\">\n"; getDirectory("$path/$file", ($level+1)); } else { // Just print out the filename echo "\t\t<file>$file</file>\n"; } } } // Close the directory handle closedir($dh); } echo "</structure>"; ?> That is outputting this: <?xml version="1.0" encoding="utf-8"?> <structure> <project name="project2"> <file>edit_client_info.png</file> <file>loading16x16listFM.gif</file> <file>nice interface.psd</file> <project name="project1"> <file>edit_client_info.png</file> <file>loading16x16listFM.gif</file> <file>nice interface.psd</file> </structure> Any ideas what I need to change? Quote Link to comment https://forums.phpfreaks.com/topic/172195-solved-help-with-recursive-folder-function/ Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 Use DOMDocument it's much easier and can be used in a recursive function when the function is done you just $dom->saveXml(); Quote Link to comment https://forums.phpfreaks.com/topic/172195-solved-help-with-recursive-folder-function/#findComment-907907 Share on other sites More sharing options...
rondog Posted August 27, 2009 Author Share Posted August 27, 2009 ok I think I understand it, but I am getting an error: Fatal error: Call to a member function appendChild() on a non-object on line 40 my code: <?php header("content-type: application/xml; charset=ISO-8859-15"); $xml = new DOMDocument("1.0", "ISO-8859-15"); $xml_structure = $xml->createElement("structure"); getDirectory("clients/xyzclient1"); function getDirectory($path = '.', $level = 0) { // Directories to ignore when listing output. Many hosts // will deny PHP access to the cgi-bin. $ignore = array('cgi-bin', '.', '..'); // Open the directory to the handle $dh $dh = @opendir($path); // Loop through the directory while (false !== ($file = readdir($dh))) { // Check that this file is not to be ignored if (!in_array($file, $ignore)) { // Just to add spacing to the list, to better // show the directory tree. $spaces = str_repeat(' ', ($level * 4 )); // Its a directory, so we need to keep reading down... if (is_dir("$path/$file")) { // Re-call this same function but on a new directory. // this is what makes function recursive. $xml_project = $xml->createElement("project"); $xml_project->setAttribute("name", "$file"); //<---- LINE 40 getDirectory("$path/$file", ($level+1)); } else { // Just print out the filename $xml_file = $xml->createElement("file","$file"); // echo "\t\t<file>$file</file>\n"; } } $xml_project->appendChild( $xml_file ); $xml_structure->appendChild( $xml_project ); } // Close the directory handle $xml->appendChild( $xml_structure ); print $xml->saveXML(); closedir($dh); } Quote Link to comment https://forums.phpfreaks.com/topic/172195-solved-help-with-recursive-folder-function/#findComment-907920 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 function directory2xml(&$xmlRoot, $directory, $recursive = true, $ignore = array()) { if (!is_dir($directory) || !is_readable($directory)) return false; $files = scandir($directory); foreach ($files as $file) { if ('.' === $file[0] || in_array($file, $ignore)) continue; $fullpath = implode(DIRECTORY_SEPARATOR, array($directory, $file)); if (is_file($fullpath)) { $xmlRoot->appendChild($xmlRoot->createElement('file', $fullpath)); } else if ($recursive) { $project = $xmlRoot->createElement('project'); $project->setAttribute('name', $file); directory2xml($project, $fullpath, $recursive, $ignore); } } } $dom = new DomDocument('1.0', 'ISO-8859-1'); $root = $dom->appendChild($dom->createElement('structure')); directory2xml($root, realpath('path/to/directory'), true, array('cgi-bin')); print $dom->saveXml(); Quote Link to comment https://forums.phpfreaks.com/topic/172195-solved-help-with-recursive-folder-function/#findComment-907929 Share on other sites More sharing options...
rondog Posted August 27, 2009 Author Share Posted August 27, 2009 I appreciate your help so far ignace. I am hoever getting a new error: Fatal error: Call to undefined method DOMElement::createElement() on line 13 Line 13 is this line: $project = $xmlRoot->createElement('project'); Does this dom stuff come with PHP or is it an extension? edit::scratch that my server has DOM/XML enabled Quote Link to comment https://forums.phpfreaks.com/topic/172195-solved-help-with-recursive-folder-function/#findComment-907932 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 Try: function directory2xml(&$xmlRoot, $directory, $recursive = true, $ignore = array()) { if (!is_dir($directory) || !is_readable($directory)) return false; $files = scandir($directory); foreach ($files as $file) { if ('.' === $file[0] || in_array($file, $ignore)) continue; $fullpath = implode(DIRECTORY_SEPARATOR, array($directory, $file)); if (is_file($fullpath)) { $xmlRoot->appendChild(new DomElement('file', $fullpath)); } else if ($recursive) { $project = new DomElement('project'); $project->setAttribute('name', $file); directory2xml($project, $fullpath, $recursive, $ignore); } } } $dom = new DomDocument('1.0', 'ISO-8859-1'); $root = $dom->appendChild($dom->createElement('structure')); directory2xml($root, realpath('path/to/directory'), true, array('cgi-bin')); print $dom->saveXml(); Quote Link to comment https://forums.phpfreaks.com/topic/172195-solved-help-with-recursive-folder-function/#findComment-907933 Share on other sites More sharing options...
rondog Posted August 27, 2009 Author Share Posted August 27, 2009 A new error arises ...this one is a doozy Fatal error: Uncaught exception 'DOMException' with message 'No Modification Allowed Error' in /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php:14 Stack trace: #0 /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php(14): DOMElement->setAttribute('name', 'project1') #1 /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php(22): directory2xml(Object(DOMElement), '/homepages/26/d...', true, Array) #2 {main} thrown in /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php on line 14 Line 14 is: $project->setAttribute('name', $file); Quote Link to comment https://forums.phpfreaks.com/topic/172195-solved-help-with-recursive-folder-function/#findComment-907934 Share on other sites More sharing options...
rondog Posted August 27, 2009 Author Share Posted August 27, 2009 I keep googling these errors, but cant seem to find the answer Quote Link to comment https://forums.phpfreaks.com/topic/172195-solved-help-with-recursive-folder-function/#findComment-907941 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 A new error arises ...this one is a doozy Fatal error: Uncaught exception 'DOMException' with message 'No Modification Allowed Error' in /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php:14 Stack trace: #0 /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php(14): DOMElement->setAttribute('name', 'project1') #1 /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php(22): directory2xml(Object(DOMElement), '/homepages/26/d...', true, Array) #2 {main} thrown in /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php on line 14 Line 14 is: $project->setAttribute('name', $file); Woeps sorry: function directory2xml(&$xmlRoot, $directory, $recursive = true, $ignore = array()) { if (!is_dir($directory) || !is_readable($directory)) return false; $files = scandir($directory); foreach ($files as $file) { if ('.' === $file[0] || in_array($file, $ignore)) continue; $fullpath = implode(DIRECTORY_SEPARATOR, array($directory, $file)); if (is_file($fullpath)) { $xmlRoot->appendChild(new DomElement('file', $fullpath)); } else if ($recursive) { $project = $xmlRoot->appendChild(new DomElement('project')); $project->setAttribute('name', $file); directory2xml($project, $fullpath, $recursive, $ignore); } } } $dom = new DomDocument('1.0', 'ISO-8859-1'); $root = $dom->appendChild($dom->createElement('structure')); directory2xml($root, realpath('path/to/directory'), true, array('cgi-bin')); print $dom->saveXml(); Check if that works I know what the problem is not sure this solves it. Quote Link to comment https://forums.phpfreaks.com/topic/172195-solved-help-with-recursive-folder-function/#findComment-907949 Share on other sites More sharing options...
rondog Posted August 27, 2009 Author Share Posted August 27, 2009 success! You are awesome man. I appreciate it so much! One question..How do I get rid of the full path thing? The file node comes out like this: /homepages/26/d220262095/htdocs/flashden/clientlogin2/clients/xyzclient1/project1/edit_client_info.png and my webroot is htdocs. What I would like it to be is just 'xyzclient1/project1/edit_client_info.png' Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/172195-solved-help-with-recursive-folder-function/#findComment-907962 Share on other sites More sharing options...
rondog Posted August 27, 2009 Author Share Posted August 27, 2009 ah nevermind..I removed the realpath method and that worked..thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/172195-solved-help-with-recursive-folder-function/#findComment-907963 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.