We help IT Professionals succeed at work.

Php, Get different arrays from class

Pedro Chagas
Pedro Chagas asked
on
Hi E's, the code in snippet code show in the browser all folders and all files inside specific folder.
The output is one array with two different keys, directories and files, and the problem is that information comes together.
So, because that I need to separate directories and files, and I need to put that information inside this arrays: array_directories and array_files.
How I do that?

Regards, JC
<?php 
    //require_once("dir.class.php"); 
    $obj = new read_full_dir('../../upload/'); 
        echo "<pre>"; 
    print_r($obj->getTree());  
         echo "</pre>"; 
          
class read_full_dir 
{ 
  var $dir_tree; 
  /*================================================ 
     class constructor 
     ================================================*/ 
  function read_full_dir( $path_to_dir ) 
  { 
    $this->read_directory( $path_to_dir ); 
  } 
  /*================================================ 
      reads the full directory tree and store it in 
      $dir_tree 
     ================================================*/ 
  function read_directory( $directory ) 
  { 
    if ( $handle =     @opendir( $directory ) ) 
    { 
    while ( false !== ( $file = readdir( $handle ) ) ) 
    { 
        if ( $file != ".." && $file != "." ) 
      { 
        if( is_dir( $directory.'/'.$file)) 
        { 
          $this->dir_tree['directories'][] = $directory.'/'.$file; 
          $this->read_directory( $directory.'/'.$file ); 
        } 
        else 
          $this->dir_tree['files'][] = $directory.'/'.$file; 
      } 
    } 
    } 
  } 
   
  function getTree(){ 
      return $this->dir_tree; 
  } 
  
} 
?>

Open in new window

Comment
Watch Question

Hello joaochagas,

You can separate final result as following

$arr_tree = $obj->getTree();

$arr_directories = $arr_tree['directories'];
$arr_files = $arr_tree['files'];

Hope this will help you.

Thank You.

Amar.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.