Для меня более чистым решением было бы:
public static string GetLocalIP()
{
string ipv4Address = String.Empty;
foreach (IPAddress currentIPAddress in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (currentIPAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ipv4Address = currentIPAddress.ToString();
break;
}
}
return ipv4Address;
}
Я рекомендую использовать DirectoryIterator для создания вашего массива
Вот фрагмент, который я собрал очень быстро, но в настоящее время у меня нет среды для его тестирования, поэтому будьте готовы отлаживать его.
$fileData = fillArrayWithFileNodes( new DirectoryIterator( '/path/to/root/' ) );
function fillArrayWithFileNodes( DirectoryIterator $dir )
{
$data = array();
foreach ( $dir as $node )
{
if ( $node->isDir() && !$node->isDot() )
{
$data[$node->getFilename()] = fillArrayWithFileNodes( new DirectoryIterator( $node->getPathname() ) );
}
else if ( $node->isFile() )
{
$data[] = $node->getFilename();
}
}
return $data;
}
Простая реализация без обработки ошибок:
function dirToArray($dir) {
$contents = array();
# Foreach node in $dir
foreach (scandir($dir) as $node) {
# Skip link to current and parent folder
if ($node == '.') continue;
if ($node == '..') continue;
# Check if it's a node or a folder
if (is_dir($dir . DIRECTORY_SEPARATOR . $node)) {
# Add directory recursively, be sure to pass a valid path
# to the function, not just the folder's name
$contents[$node] = dirToArray($dir . DIRECTORY_SEPARATOR . $node);
} else {
# Add node, the keys will be updated automatically
$contents[] = $node;
}
}
# done
return $contents;
}
Я добился успеха с пакетом PEAR File_Find, в частности методом mapTreeMultiple () . Ваш код будет выглядеть примерно так:
require_once 'File/Find.php';
$fileList = File_Find::mapTreeMultiple($dirPath);
Это должно вернуть массив, подобный тому, который вы просите.