sw0o0sh Posted June 23, 2010 Share Posted June 23, 2010 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? Link to comment https://forums.phpfreaks.com/topic/205691-turning-only-directories-into-an-array-through-a-recursive-loop-function/ Share on other sites More sharing options...
sw0o0sh Posted June 23, 2010 Author Share Posted June 23, 2010 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? Link to comment https://forums.phpfreaks.com/topic/205691-turning-only-directories-into-an-array-through-a-recursive-loop-function/#findComment-1076318 Share on other sites More sharing options...
Andy-H Posted June 24, 2010 Share Posted June 24, 2010 <?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? Link to comment https://forums.phpfreaks.com/topic/205691-turning-only-directories-into-an-array-through-a-recursive-loop-function/#findComment-1076329 Share on other sites More sharing options...
Andy-H Posted June 24, 2010 Share Posted June 24, 2010 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>'; Link to comment https://forums.phpfreaks.com/topic/205691-turning-only-directories-into-an-array-through-a-recursive-loop-function/#findComment-1076344 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.