CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    Dec 2009
    Posts
    1

    PHP - writing a directory into an XML file

    I am having trouble writing my file directory of my website in to an XML file. I've searched all over google and have had no success with the examples that I've found. I've checked the php documentation but can't find anything really.

    here's the code i've been working with:

    PHP Code:
    $contents = "<?xml version=\"1.0\" ?>\n";
    $path = "easythanks/";

    function outputPath($path){
      $level=substr_count($path,'/');
      for ($i=1;$i<$level;$i++) echo '   ';
      echo basename($path);
      echo "\n";
    }

    echo '<pre>';
    traverseDirTree('./','outputpath','outputpath');
    echo '</pre>';


    function traverseDirTree($base,$fileFunc,$dirFunc=null,$afterDirFunc=null){
      $subdirectories=opendir($base);
      while (($subdirectory=readdir($subdirectories))!==false){
        $path=$base.$subdirectory;
        if (is_file($path)){
            $contents .= "<item text=".$path." />";
        
              if ($fileFunc!==null){
              $fileFunc($path);
          }
        }else{
            $contents .= "<item text=".$path.">";
          if ($dirFunc!==null) $dirFunc($path);
          if (($subdirectory!='.') && ($subdirectory!='..')){
            
            traverseDirTree($path.'/',$fileFunc,$dirFunc,$afterDirFunc);
            
          }
          if ($afterDirFunc!==null) $afterDirFunc($path);
          $contents .= "</item>";
        }
      }
    }

    $myFile = "test.xml";
    $fh = fopen($myFile, 'a') or die("can't open file");
    fwrite($fh, $contents);
    fclose($fh);
    Can anyone please point me in the right direction? PS the xml file needs to look like this

    Code:
    <item text="directory1">
                <item text="directory2">
                    <item text="child1" />
                </item>
                <item text="child2" />
                <item text="child3"/>
    </item>
    Thanks,
    David
    Last edited by PeejAvery; December 17th, 2009 at 10:30 AM. Reason: Added code tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured