473,463 Members | 1,552 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Filesystem to XML class function is giving incorrect output.

162 100+
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
3 1859
empiresolutions
162 100+
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
5,821 Expert 4TB
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
3,237 Expert 2GB
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

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

Similar topics

9
by: Peter Hansen | last post by:
The term "mock filesystem" refers to code allowing unit or acceptance tests to create, read and write, and manipulate in other ways "virtual" files, without any actual disk access. Everything is...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
14
by: junw2000 | last post by:
Is it possible to create an abstract base class without a pure virtual function? Thanks Jack
3
by: Ron Jackson | last post by:
I am using Python 2.5 on Windows XP. I have installed Pyserial and win32all extensions. When I try to run the example program scan.py (included below), or any other program using pyserial, as...
5
by: GaryE | last post by:
Hello: I am having trouble linking a couple of files using the boost::filesystem. I am using MSVC 6.0. Here is an abbreviated version of my problem: foo.h: #ifndef __FOO_ #define...
17
by: venkat | last post by:
Hi, I have written a program void main() { printf("%d %d\n", sizeof main, sizeof(main())); } in this program the output is 1 and 4,
5
by: Ben | last post by:
Hi, i defined a function in the base class 'ford' and the same function (with different output) in subclass "peugeot". I first put 'Overridable function' in the base class and 'Overrides...
1
by: david.katkowski | last post by:
I'm trying to use the __builtin__ filter function within a class; however, I receive the following error: NameError: global name 'MajEthnic' is not defined The line of code is: EthMaj =...
2
by: Oli Thissen | last post by:
Hello everybody! I'm having a little problem with the following: I wrote a class to collect data (Let's call it DataCollector). Now I want the data to be interpreted in various ways. My idea was...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.