Skip to content

Instantly share code, notes, and snippets.

@jrobinsonc
Last active December 15, 2015 21:19
Show Gist options
  • Save jrobinsonc/5324874 to your computer and use it in GitHub Desktop.
Save jrobinsonc/5324874 to your computer and use it in GitHub Desktop.
Get the directory contents recursively. #directories #files #php

Get directory contents

Get the directory contents recursively.

Usage

require 'get_dir.php';

printf('<ul>');

foreach(get_dir('/path/to/directory') as $item)
{
    printf('<li>%s</li>', $item);
}

printf('</ul>');
<?php
/**
* Get the directory contents recursively.
*
* @author JoseRobinson.com
* @link GitHup: https://gist.github.com/5324874
* @version 201304062329
* @param string $dir
* @return array
*/
function get_dir($dir)
{
$files_list = array();
foreach(scandir($dir) as $file)
{
if (in_array($file, array('.', '..'))) continue;
$full_path = "{$dir}/{$file}";
if (is_dir($full_path))
{
$files_list = array_merge($files_list, get_dir($full_path));
}
else
{
$files_list[] = realpath($full_path);
}
}
return $files_list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment