note 114116 added to function.scandir
| From: | Patrick | Date: | Sun, 12 Jan 2014 21:10:07 +0000 |
| Subject: | note 114116 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198216@lists.php.net to get a copy of this message | ||
Here is my recursive function, placing directories into new array keys and its contents within.
/* find_files( string, &array )
** Recursive function to return a multidimensional array of folders and files
** that are contained within the directory given
*/
function find_files($dir, &$dir_array)
{
// Create array of current directory
$files = scandir($dir);
if(is_array($files))
{
foreach($files as $val)
{
// Skip home and previous listings
if($val == '.' || $val == '..')
continue;
// If directory then dive deeper, else add file to directory key
if(is_dir($dir.'/'.$val))
{
// Add value to current array, dir or file
$dir_array[$dir][] = $val;
find_files($dir.'/'.$val, $dir_array);
}
else
{
$dir_array[$dir][] = $val;
}
}
}
ksort($dir_array);
}
// Example
$folder_list = array();
find_files('/path', $folder_list)
var_dump($folder_list);
array(3) {
["directory_01"]=>
array(4) {
[0]=>
string(12) "directory_02"
[1]=>
string(12) "directory_03"
[2]=>
string(11) "file_01.txt"
[3]=>
string(11) "file_02.txt"
}
["directory_01/directory_02"]=>
array(2) {
[0]=>
string(11) "file_03.txt"
[1]=>
string(11) "file_04.txt"
}
["directory_01/directory_03"]=>
array(2) {
[0]=>
string(11) "file_05.txt"
[1]=>
string(11) "file_06.txt"
}
}
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/function.scandir.php
Edit -- https://master.php.net/note/edit/114116
Del: integrated -- https://master.php.net/note/delete/114116/integrated
Del: useless -- https://master.php.net/note/delete/114116/useless
Del: bad code -- https://master.php.net/note/delete/114116/bad+code
Del: spam -- https://master.php.net/note/delete/114116/spam
Del: non-english -- https://master.php.net/note/delete/114116/non-english
Del: in docs -- https://master.php.net/note/delete/114116/in+docs
Del: other reasons-- https://master.php.net/note/delete/114116
Reject -- https://master.php.net/note/reject/114116
Search -- https://master.php.net/manage/user-notes.php