PHP 8.5.0 Alpha 4 available for testing

Voting

: max(eight, nine)?
(Example: nine)

The Note You're Voting On

mrlemonade
14 years ago
Get all files on recursive directories in single array.

<?php
/*
* mrlemonade ~
*/

function getFilesFromDir($dir) {

$files = array();
if (
$handle = opendir($dir)) {
while (
false !== ($file = readdir($handle))) {
if (
$file != "." && $file != "..") {
if(
is_dir($dir.'/'.$file)) {
$dir2 = $dir.'/'.$file;
$files[] = getFilesFromDir($dir2);
}
else {
$files[] = $dir.'/'.$file;
}
}
}
closedir($handle);
}

return
array_flat($files);
}

function
array_flat($array) {

foreach(
$array as $a) {
if(
is_array($a)) {
$tmp = array_merge($tmp, array_flat($a));
}
else {
$tmp[] = $a;
}
}

return
$tmp;
}

// Usage
$dir = '/data';
$foo = getFilesFromDir($dir);

print_r($foo);
?>

<< Back to user notes page

To Top