We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28033
    • 925 Posts
    I’m wanting to show the total number of files in a section of my gallery, but all the code I’ve found online only works for a single directory. Is it possible at all to do what’s in the topic title w/ PHP?
      My Snippets
      -> PopUpChunk v1.0
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Technically that should be "recursion". Recursive would be an adjective: a recursive function to implement recursion. Basically it’s a function that keeps calling itself (using its output as a parameter) until some specified point is reached. See the UltimateParent snippet for a simple example of recursion; it even has an optional parameter to specify a desired stopping point.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 5689
        • 289 Posts
        Soshite, I wrote a pretty decent function a while back to find all of the files of a certain type(s) in a directory. It supports recursion like you need. With some slight modifications you could easily use it to count the images instead of get their names. Or you could just use count() on the array of filenames it returns.

        I don’t know how good you are with PHP, so if you only have some basic PHP skills, I’d be glad to modify it for you and give you a function that just counts them for you. Just let me know.

        Here is the code. I wrote it to make a simple gallery, so you can remove the code that isn’t part of the function. I’m leaving it in for now so you can get example of how to use it, though. It’s fairly well documented in the comments, but if you need help with it, I’ll be glad to do that, too.

        <?php
        	//gets images; see below for explanation of function use
        	$files = get_files(array('jpg','png'));
        	
        	//exports HTML
        	if(is_array($files)) {
        		foreach($files as $this_file) {
        			echo('<img alt="" src="' . $this_file . '" /><br />');
        		}
        	}
        	
        	//*** the get_files function finds of all files of $type (can be array of values or just single value) in $directory (must only be single value, defaults to current folder)
        	//*** specifiying $case_sensitive makes the types case sensitive or not (default false), so if it's true then 'JPG' will not match 'jpg'
        	//*** you can specify the depth of folders to include (default is current folder only, or 0); specify -1 for infinite depth
        	function get_files($type = null, $case_sensitive = false, $depth = 0, $directory = '.') { //gets files of certain type(s) in certain directory
        		if(is_dir($directory)) { //check if specified directory is directory			
        			if($handle = opendir($directory)) { //open directory and check if open was successful				
        				$files = array(); //set files to empty array
        			    while(($this_file = readdir($handle)) !== false) { //loop through files
        			        if($this_file == '.' || $this_file == '..') { continue; } //check if file is current directory or parent directory and continue					
        					$this_file = $directory . (substr($directory, strlen($directory) - 1, 1) == '/' ? '' : '/') . $this_file; //set file to path of file
        					if(is_dir($this_file)) { //check if file is directory						
        						if($depth > 0 || $depth == -1) { //check if depth is greater than 0 or infinite
        							$these_files = array(); //set these files to empty array
        							$these_files = get_files($type, $case_sensitive, ($depth == -1 ? -1 : $depth - 1), $this_file); //get files in directory
        							if(is_array($these_files)) { //check if files is array
        								foreach($these_files as $this2_file) { //loop through files
        									$files[] = $this2_file; //add file to files
        								}
        							}
        						}
        					} else { //file is not directory						
        						if(is_null($type)) { //check if type is not specified
        							$files[] = $this_file; //add file to files							
        						} else { //type is specified							
        							if(!is_array($type)) { //check if type is not array
        								$type = array($type); //set type to new array with type								
        							}
        							foreach($type as $this_type) { //loop through types								
        								$expression = '\.' . $this_type . '$';
        								if($case_sensitive) { //check if types are case sensitive									
        									if(ereg($expression, $this_file)) { //check if file is correct type
        										$files[] = $this_file; //add file to files
        										break;
        									}
        								} else { //types are not case sensitive									
        									if(eregi($expression, $this_file)) { //check if file is correct type										
        										$files[] = $this_file; //add file to files
        										break;
        									}
        								}							
        							}
        						}
        					}
        			    }
        				closedir($handle); //close directory				
        				return($files); //return files
        			}			
        		}
        		return(false); //return false
        	}
        ?>
        


        I hope this helps!
          I&#39;m learning more about MODx all the time and loving it.
          • 3749
          • 24,544 Posts
          Quote from: sottwell at May 14, 2009, 01:48 AM

          Technically that should be "recursion". Recursive would be an adjective: a recursive function to implement recursion. Basically it’s a function that keeps calling itself (using its output as a parameter) until some specified point is reached. See the UltimateParent snippet for a simple example of recursion; it even has an optional parameter to specify a desired stopping point.

          Hey -- are you trying to steal my job as the official MODx annoying language weenie? wink
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Grammar Granny!
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 3749
              • 24,544 Posts
              LOL wink
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting