-2

我试图编辑这个 PHP 代码以指定一个目录来递归列出文件和子目录,但没有任何乐趣。

任何想法都非常感谢。

我刚刚从这个链接中找到了这段代码:

PHP列出目录中的所有文件

这是下面的代码:

    <?php

    /**
    * Recovers folder structure and files of a certain path
    * 
    * @param string $path Folder where files are located
    * @param string $pattern Filter by extension
    * @param string $flags Flags to be passed to the glob
    * @return array Folder structure
    */


    function getFolderTree($path)
    {
    //Recovers files and directories
    //$paths = glob($path  . "*", GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT);
//@param string ($path ".\temp" );
//$path = ".";

    $paths = glob($path . "*",  GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT);
    $files = glob($path . "*");


    //Traverses the directories found
    foreach ($paths as $key => $path)
    {
    //Create directory if exists
    //$directory = explode("//", $path);
    $directory = explode("\\", $path);
    unset($directory[count($directory) - 1]);
    $directories[end($directory)] = getFolderTree($path);

    //Verify if exists files
    foreach ($files as $file)
    {
        if (strpos(substr($file, 2), ".") !== false)
            $directories[] = substr($file, (strrpos($file, "\\") + 1));
    }
    }

    //Return the directories
    if (isset($directories))
    {
    return $directories;
    }
    //Returns the last level of folder
    else
    {
    $files2return = Array();
    foreach ($files as $key => $file)
        $files2return[] = substr($file, (strrpos($file, "\\") + 1));
    return $files2return;
    }
    }

    /**
    * Creates the HTML for the tree
    * 
    * @param array $directory Array containing the folder structure
    * @return string HTML
    */
    function createTree($directory)
    {
    $html = "<ul>";
    foreach($directory as $keyDirectory => $eachDirectory)
    {
    if(is_array($eachDirectory))
    {
        $html .= "<li class='closed'><span class='folder'>" . $keyDirectory .  "</span>";
        $html .= createTree($eachDirectory);
        $html .=  "</li>";
    }
    else
    {
        $html .= "<li><span class='file'>" . $eachDirectory . "</span></li>";
    }
    }
    $html .= "</ul>";

    return $html;
    }

    //Create output

    $pathx = 'temp/';
    //$directory = getFolderTree($pathx);
    $directory = getFolderTree($pathx );

    $htmlTree = createTree($directory[$pathx]);
    ?>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
    <title>PHP Directories</title>

    <link rel="stylesheet" href="http://jquery.bassistance.de/treeview/jquery.treeview.css" />
    <link rel="stylesheet" href="http://jquery.bassistance.de/treeview/demo/screen.css" />

    <script src="http://jquery.bassistance.de/treeview/lib/jquery.js" type="text/javascript"></script>
    <script src="http://jquery.bassistance.de/treeview/lib/jquery.cookie.js" type="text/javascript"></script>
    <script src="http://jquery.bassistance.de/treeview/jquery.treeview.js" type="text/javascript"></script>

    <script type="text/javascript" src="http://jquery.bassistance.de/treeview/demo/demo.js"></script>

    </head>
    <body>
    <div id="main">
        <ul id="browser" class="filetree">
            <?php echo $htmlTree;?>
        </ul>
    </div>
    </body>
    </html>
4

1 回答 1

0

您的代码中有两个问题。

第一个是目录分隔符 - 您应该使用常量DIRECTORY_SEPARATOR而不是\,例如:

$directory = explode(DIRECTORY_SEPARATOR, $path);

这将使您的代码不仅在 Windows 上有效,而且在 unix 系统上有效。

第二个问题在这里:

$htmlTree = createTree($directory[$pathx]);

数组$pathx中没有键。$directory可能它应该看起来像这样:

$htmlTree = createTree($directory);

但请注意,很难说出您期望什么结果以及您得到什么错误。

于 2013-07-03T07:17:03.223 回答