0

我写了一个,通过它我想要一个包含所有和的php function数组。filesdirectoriesdirectory


1)下面是我的目录结构 -

/pages  (Full path - /home/ubuntu/projects/xyz.com/code/ciapp/views/pages)
|
|--file1.php
|--file2.php
|--file3.php
|--folder1
   |--file4.php
   |--file5.php
   |--folder2
      |--file6.php
|--folder3
   |--file7.php
   |--file8.php


2)我想要的arrayfiles and directories-

Array
(
[files] => Array
    (
        [0] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/file1.php
        [1] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/file2.php
        [2] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/file3.php
        [3] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/folder1/file4.php
        [4] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/folder1/file5.php
        [5] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/folder1/folder2/file6.php
        [6] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/folder3/file7.php
        [7] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/folder3/file8.php
    )

[directories] => Array
    (
        [0] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/folder1
        [1] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/folder1/folder2
        [2] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/folder3
    )
)


3)我得到的(只有1级)-

Array
(
[files] => Array
    (
        [0] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/file1.php
        [1] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/file2.php
        [2] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/file3.php
    )

[directories] => Array
    (
        [0] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/folder1
        [1] => /home/ubuntu/projects/xyz.com/code/ciapp/views/pages/folder3
    )
)


4)我的功能 -

function get_files_directories($directory, $a = array())
{
    $dirs = scandir($directory, 1);
    $arr = $a;

    foreach($dirs as $dir)
    {
       if($dir != "." && $dir != '..')
       {
           $new_dir = $directory.'/'.$dir;
           if(is_dir($new_dir))
           {
               $arr['directories'][] = $new_dir;
               get_files_directories($new_dir, $arr);
           }
           else
           {
               $arr['files'][] = $new_dir;
           }
       }
    }

    return $arr;
}

$my_dir = "/home/ubuntu/projects/xyz.com/code/ciapp/views/pages";
$files_dirs = get_files_directories($my_dir);

echo '<pre>'; print_r($files_dirs);


我在这里想念什么?帮助将不胜感激。谢谢 :)

4

4 回答 4

5

切换$path您的路径,这应该可以。SPL 包含递归目录遍历所需的所有类,无需自己编写。

<?php

$path   = '.';
$result = array('files' => array(), 'directories' => array());

$DirectoryIterator = new RecursiveDirectoryIterator($path);
$IteratorIterator  = new RecursiveIteratorIterator($DirectoryIterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($IteratorIterator as $file) {

    $path = $file->getRealPath();
    if ($file->isDir()) {
        $result['directories'][] = $path;
    } elseif ($file->isFile()) {
        $result['files'][] = $path;
    }

}

print_r($result);

有关更多信息,请参阅 PHP 文档:RecursiveDirectoryIteratorRecursiveIteratorIteratorSplFileInfo是很好的起点!

于 2014-04-21T14:07:17.553 回答
1

使用FilesystemIterator可能会快一点:

function getDirectoryList($dir) {
    $dirList = $fileList = array();
    $iter = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);

    foreach($iter as $file) {
        if($file->isDir()) {
            $dirList[$file->getFilename()] = getDirectoryList($file->getPathname());
        } else {
            $fileList[$file->getFilename()] = $file->getFilename();
        }
    }
    uksort($dirList, "strnatcmp");
    natsort($fileList);
    return $dirList + $fileList;
}

此函数将为您提供一个结构与您的目录结构完全相同的多维数组,这也可能更易于使用。

于 2014-04-21T14:00:24.250 回答
0

这是一个递归调用,您可以将返回值附加到数组或处理数组引用:

if(is_dir($new_dir))
{
    $arr['directories'][] = $new_dir;
    get_files_directories($new_dir, $arr); //assign or return
}

-

function get_files_directories($directory, &$arr) 
于 2014-04-21T13:56:40.273 回答
0

您应该分配从递归调用返回的“arr”,或者您应该通过引用传入数组。(参见通过引用传递:http: //php.net/manual/en/language.references.pass.php

通过引用传递: get_files_directories($new_dir, &$arr);

于 2014-04-21T14:03:31.887 回答