Jump to content

Turning only directories into an array through a recursive loop function


sw0o0sh

Recommended Posts

I'm currently working on a PHP ftp program. I've made a lot of progress, but I'm stuck at a specific part, the part my topic title says. I've done this before (back in the day), but it was a little ghetto. It would do an echo from inside the function, and I don't want to approach it like that anymore. I would like to put the directory names into a one dimensional array and then return the array.

 

But for the discussion, this code worked (and still does), on my machine:

<?php
function readAllDir($dir){
	$dh = opendir($dir);
	while (($file = readdir($dh)) !== false) {
		if(is_dir($dir . $file)){
			if($file == '.' || $file == '..'){} else {
				echo $dir . $file . "/";
				readAllDir($dir . $file . "/");	
			}
		}
	}
	closedir($dh);
}
?>

 

Now here's my approach with the array, which for some reason, never loops through the rest, (it'll return the directories.. on say.. ./ for example, but it never goes through the directories IN ./ and so on and so forth).

 

<?php
function readAllDir($dir){
	$dh = opendir($dir);
	$file_array = array();
	while (($file = readdir($dh)) !== false) {
		if(is_dir($dir . $file)){
			if($file == '.' || $file == '..'){} else {
				readAllDir($dir . $file . "/");	
				$file_array[] = $dir . $file . "/";
			}
		}
	}
	return $file_array;
}
?>

 

What am I doing wrong?

Here is my second approach at accomplishing what I am trying to do. Seeing as the array didn't work, I tried turning all the directories into a long string separated by a "|" symbol.

 

<?php
	function recursiveDir($path, $returnstring = null){
		$returnstring = isset($returnstring) ? $returnstring : null;
		if($handle = opendir($path)){
			while(($fn = readdir($handle)) !== false){
				$curr_path = $path . $fn;
				if(is_dir($curr_path)){
					if($fn == '.' || $fn == '..'){} else {
						$returnstring .= $curr_path . "/|";
						$send_next = recursiveDir($curr_path . "/", $returnstring);
					}
				}
			}
			closedir($handle);
		}
		return $returnstring;
	}
?>

Now, it's just not stacking up and returning. It doesn't return anything for that matter. But, if I change this line:

 

$returnstring .= $curr_path . "/|";

 

To

 

echo $curr_path . "/|";

 

(Purely for a debugging test), it will show all the directories it's looping through. So I know at least THAT much is working. But for some reason when I try stacking it all into one long string, it just isn't working. Any clues?

<?php

   function readAllDir($dir){
      static $dirs = array();
      
      $dh = opendir($dir);
      while (($file = readdir($dh)) !== false) {
         if(is_dir($dir . $file)){
            if($file == '.' || $file == '..'){} else {
               $dirs[] = $dir . $file . '/';
               readAllDir($dir . $file . "/");   
            }
         }
      }
      closedir($dh);
      return $dirs;
   }
   
   $directories = readAllDir('/');
   echo '<pre>' . print_r($directories, true) . '</pre>';
   ?>

 

Wouldn't glob be a better (simpler) approach?

function getDirs($dir)
{
   static $dirs = array();
   
   $dir = (substr($dir, -1) == '/') ? ($dir) : ($dir . '/');
   
   $dirs[$dir] = array_filter(glob($dir . '*'), 'is_dir');
   
   foreach($dirs[$dir] as $curDir)
   {
      getDirs($curDir);
   }

   return array_filter($dirs, 'count');
}

echo '<pre>' . print_r(getDirs('/wamp/www/'), true) . '</pre>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.