Mikael-Sundstrom
11/9/2017 - 4:47 PM

PHP Environment Functions

print_r(dirToArray$the_directory));

function dirToArray($dir) {
  $result = array();
  
  $cdir = scandir($dir);
  foreach ($cdir as $key => $value)
  {
    if (!in_array($value,array(".","..")))
    {
      if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
      {
        $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
      }
      else
      {
        $result[] = $value;
      }
    }
  }
  
  return $result;
}

/*
Output

Array
(
  [subdir1] => Array
  (
    [0] => file1.txt
    [subsubdir] => Array
    (
      [0] => file2.txt
      [1] => file3.txt
    )
  )
  [subdir2] => Array
  (
    [0] => file4.txt
  )
)
*/
function get_files_without_dots($dir) {
  $files = array_slice(scandir($dir), 2);

  return $files;
}
function countFiles($dir){
  $files = array();
  $directory = opendir($dir);
  
  while($item = readdir($directory)) {
    // We filter the elements that we don't want to appear ".", ".." and ".svn"
    if(($item != ".") && ($item != "..") && ($item != ".svn") ) {
      $files[] = $item;
    }
  }
  
  $numFiles = count($files);
  return $numFiles;
}