By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,808 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,808 IT Pros & Developers. It's quick & easy.

Filesystem to XML class function is giving incorrect output.

100+
P: 162
I'm trying to build my first PHP Class. After days of tweaking, im lost. I am used to working with functions and arrays, but wrapping them in classes is confusing me.

The following code is to return an Array Collection representative of a folder and sub folder in a directory of choice. It doesn't work. Throws an error "Call to undefined function: parse_dir()". It would be great if someone out there could see where im not correct.

I've been trying to follow the example provided at http://www.sephiroth.it/tutorials/fl...ct/page003.php
Expand|Select|Wrap|Line Numbers
  1. <?
  2. class Tree {
  3.     var $folders;
  4.     var $files;
  5.     var $name;
  6.     // explicit actionscript package
  7.     var $_explicitType = "tutorials.Tree";
  8. }
  9.  
  10. class DirTree {
  11.  
  12.     /**
  13.      * Get a tree of folders and files from a spec dir
  14.      * @returns An ArrayCollection of Tree
  15.      */
  16.     function DirTree( $dir_tree ){
  17.  
  18.         $t = array();
  19.         $_tree = parse_dir($dir_tree);
  20.  
  21.         for($a = 0; $a < count($_tree); $a++){
  22.             $tree = new Tree();
  23.             $tree->folders = $_tree[$a][0];
  24.             $tree->files = $_tree[$a][1];
  25.             $tree->name = $_tree[$a][2];
  26.             $t[] = $tree;
  27.         }
  28.  
  29.         return $t;
  30.     }
  31.  
  32.     /**
  33.      * Get a tree of folders and files from a spec dir
  34.      * @returns An Array of Tree
  35.      */
  36.     function parse_dir( $folder ){
  37.  
  38.         $dir                = @opendir( $folder );
  39.         $fname              = array_pop( explode( "/",$folder) );
  40.         $fname              = empty( $fname ) ? "root" : str_replace( " ","_",$fname );
  41.         $path               = "";
  42.         $filecount          = 0;
  43.         $foldercount        = 0;
  44.         $xml                = "";
  45.         $tree                = array();
  46.         $limb                = array();
  47.  
  48.         while ( false != ( $item = @readdir( $dir ) ) ) {
  49.  
  50.             if( $item == "." || $item == ".." ) continue;
  51.  
  52.             if( is_dir( "$folder/$item" ) ){
  53.  
  54.                 $tree[][$folder] = parse_dir( "$folder/$item");
  55.                 $foldercount++;
  56.                 $limb['folders'] = $foldercount;
  57.                 $filecount++;
  58.                 $limb['files'] = $filecount;
  59.                 continue;
  60.  
  61.             }
  62.  
  63.             $limb['name'] = $item;
  64.         }
  65.  
  66.         $tree[] = $limb;
  67.         return $tree;
  68.  
  69.     }
  70. }
  71.  
  72. $class = new DirTree("path_to_folder"); // "../../example_folder"
  73. echo $class->DirTree();
  74. ?>
  75.  
Thanks.
Jun 4 '07 #1
Share this Question
Share on Google+
3 Replies


100+
P: 162
i have made some updates from suggestions and still get errors. Here is new code.
Expand|Select|Wrap|Line Numbers
  1. <?
  2. class Tree {
  3.     var $folders;
  4.     var $files;
  5.     var $name;
  6.     // explicit actionscript package
  7.     var $_explicitType = "tutorials.Tree";
  8. }
  9.  
  10. class DirTree {
  11.  
  12.     /**
  13.      * Get a tree of folders and files from a spec dir
  14.      * @returns An ArrayCollection of Tree
  15.      */
  16.     function DirTree( $dir_tree ){
  17.  
  18.         $t = array();
  19.         $_tree = $this->parse_dir($dir_tree);
  20.  
  21.         for($a = 0; $a < count($_tree); $a++){
  22.             $tree = new Tree();
  23.             $tree->folders = $_tree[$a][0];
  24.             $tree->files = $_tree[$a][1];
  25.             $tree->name = $_tree[$a][2];
  26.             $t[] = $tree;
  27.         }
  28.  
  29.         return $t;
  30.     }
  31.  
  32.     /**
  33.      * Get a tree of folders and files from a spec dir
  34.      * @returns An Array of Tree
  35.      */
  36.     function parse_dir( $folder ){
  37.  
  38.         $dir                = @opendir( $folder );
  39.         $fname              = array_pop( explode( "/",$folder) );
  40.         $fname              = empty( $fname ) ? "root" : str_replace( " ","_",$fname );
  41.         $path               = "";
  42.         $filecount          = 0;
  43.         $foldercount        = 0;
  44.         $xml                = "";
  45.         $tree                = array();
  46.         $limb                = array();
  47.  
  48.         while ( false != ( $item = @readdir( $dir ) ) ) {
  49.  
  50.             if( $item == "." || $item == ".." ) continue;
  51.  
  52.             if( is_dir( "$folder/$item" ) ){
  53.  
  54.                 $tree[][$folder] = parse_dir( "$folder/$item");
  55.                 $foldercount++;
  56.                 $limb['folders'] = $foldercount;
  57.                 $filecount++;
  58.                 $limb['files'] = $filecount;
  59.                 continue;
  60.  
  61.             }
  62.  
  63.             $limb['name'] = $item;
  64.         }
  65.  
  66.         $tree[] = $limb;
  67.         return $tree;
  68.  
  69.     }
  70. }
  71.  
  72. $class = new DirTree();
  73.  
  74. /* view array  */
  75. echo "<pre>";
  76. print_r($class->DirTree("../../flashservices"));
  77. echo "</pre>";
  78. ?>
  79.  
Here is an XML view of what the structure should be like.
Expand|Select|Wrap|Line Numbers
  1. <folder name="root" folders="1" files="2">
  2.     <file>advancedsettings.php</file>
  3.     <file>adodbAdapter.php</file>
  4.     <folder name="adapters" folders="1" files="5">
  5.         <file>adodbAdapter.php</file>
  6.         <file>arrayfAdapter.php</file>
  7.         <file>arrayftAdapter.php</file>
  8.         <file>fbsqlAdapter.php</file>
  9.         <file>informixAdapter.php</file>
  10.         <folder name="custom" folders="0" files="2">
  11.             <file>CachedExecutionAction.php</file>
  12.             <file>CachedGateway.php</file>
  13.         </folder>
  14.     </folder>
  15. </folder>
Jun 4 '07 #2

pbmods
Expert 5K+
P: 5,821
Changed thread title to better match contents.

Looking good so far. What are you getting instead of what you wanted?
Jun 4 '07 #3

Motoma
Expert 2.5K+
P: 3,235
parse_dir() is a member of the class DirTree. As such, you will need to either call $this->parse_dir() or $obj->parse_dir() to access it.
Jun 4 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.