How to Count folders in 1 subfolder?

Discussion in 'PHP' started by Neo_The_One, Dec 1, 2006.

  1. #1
    Hello?

    I have a working php script its only php based..

    it works like this

    --- index.php
    
    ------ /Layout1
    ------ /Layout1/Cool_Layout1
    ------ /Layout1/Cool_Layout2
    ------ /Layout1/Cool_Layout3
    ------ /Layout1/Cool_Layout4
    
    ------ /Layout2
    ------ /Layout2/Cool_Layout1
    ------ /Layout2/Cool_Layout2
    ------ /Layout2/Cool_Layout3
    PHP:
    Is it possible to change the index.php to output how many folders a subfolder has?

    So to preview it in the index.php

    Layout1 (4) Layout2 (3)

    and so on and so on.. ?

    I have managed to count images in 1 subfolder, but i cannot manage to count the folders, please help?

    Thx!
     
    Neo_The_One, Dec 1, 2006 IP
  2. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #2
    Big 'G', Dec 1, 2006 IP
  3. Neo_The_One

    Neo_The_One Active Member

    Messages:
    555
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #3
    I know how to count the files in a folder, but i neeed to count the folders in a subfolder you se!?


    -- /index.php
    -- /Layouts/
    -- /Layouts/1/index.php
    -- /Layouts/2/
    -- /Layouts/3/
    -- /Layouts/4/
    -- /Layouts/5/
    PHP:
    Each subfolder, contains:

    2 images
    1 css file

    Any other ways of doing it?

    Thx!
     
    Neo_The_One, Dec 2, 2006 IP
  4. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think he's saying use is_dir() to do it recursively.. each file in a directory, pass it through is_dir(), if it returns true.. take that current path and make it go through your file-counting thing again to grab the sub-folders files.
     
    CodyRo, Dec 2, 2006 IP
  5. Neo_The_One

    Neo_The_One Active Member

    Messages:
    555
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Hmm! CodyRo! :/

    That was quit complicated, !?

    Any one care to share the complete code?

    Thx! ;)
     
    Neo_The_One, Dec 2, 2006 IP
  6. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I wrote a little chunk of example cold of how to go in recursively.. I haven't tested it but it should work (I hope?).

    The only problem I see is it'll go into a new folder as it sees it instead of listing current directories contents first, then go into subfolder and list those, etc. Could fix by some tweaking.

    
    <?php
    
    	list_dir("./");
    	
    	function list_dir($dir, $recur = false) {
    	
    		if (is_dir($dir)) {
    			
    			$dh = opendir($dir);
    			
    			while (($files = readdir($dh)) !== false) {
    			
    				if ($files == ".." || $files == ".") continue;
    				
    				if ($recur) {
    				
    					echo "SubFolder: $files <br />";
    					
    				} else {
    				
    					echo "Main Folder: $files <br />";
    				
    				}
    				
    				if (is_dir($dir . $files)) {
    				
    					list_dir($dir . $files . "/", true);
    					
    				}
    				
    			}
    			
    		}
    	
    	}
    
    ?>
    
    PHP:
     
    CodyRo, Dec 2, 2006 IP