使用 scandir() 和 sort() 进行目录索引

发布于 2025-01-07 20:13:12 字数 4721 浏览 3 评论 0原文

我稍微简化了 WAMP 提供的默认 index.php 文件,以便它可以在任何目录/位置工作。

我遇到的问题是顺序将大写字母组合在一起。

例如a,b,c,A,B,C

当我想要:A,a,B,b,C,c

<?php
date_default_timezone_set('Europe/London');
$title = 'Project Directory';
$server = 'http://' . $_SERVER['SERVER_NAME'];
$date = date('Y');

//Directory / Files
$dir    = '.';
$files = scandir($dir);

$projectsListIgnore = array ('RemoteSystemsTempFiles','icons');
$projectsFileListIgnore = array ('.project');

$projectContents = '';
$projectFileContents = '';

foreach ($files as $file) {

    if (is_dir($file) && !in_array($file,$projectsListIgnore) && substr($file,0,1) != '_' && substr($file,0,1) != '.')  {       

        $projectContents .= '<li><a class="arrow external" href="'.$file.'">'.$file.'</a></li>';

    } 

    if (is_file($file) && !in_array($file,$projectsFileListIgnore) && substr($file,0,1) != '_' && substr($file,0,1) != '.') {
        $projectFileContents .= '<li><a class="arrow icon iicon external" href="'.$file.'"><em class="ii-download '. pathinfo($file, PATHINFO_EXTENSION) .'"></em>'.$file.'</a></li>';
    } 
}


$pageContents = <<< EOPAGE
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html lang="en" xml:lang="en">
<head>
    <title>{$title}</title>
    <meta http-equiv="Content-Type" content="txt/html; charset=utf-8" />
    <link href="icons/favicon.ico" rel="icon" type="image/x-icon" />

    <style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }

    html {
        background: #ddd;
    }
    body {
        margin: 1em 10%;
        padding: 1em 3em;
        font: 70%/1.4 arial, helvetica, lucida sans, sans-serif;
        border: 1px solid #999;
        background: #eee;
        position: relative;
    }
    #head {
        margin-bottom: 1.8em;
        margin-top: 1.8em;
        padding-bottom: 0em;
        border-bottom: 1px solid #999;
        letter-spacing: -500em;
        text-indent: -500em;
        height: 125px;
    }
    h2 {
        margin: 0.8em 0 0 0;
    }
    ul {
        list-style: none;
        margin: 0;
        padding: 0;
    }
    ul.projects, ul.tools, ul.files {
        list-style: none;
        line-height: 24px;
    }
    ul.aliases a, ul.projects a, ul.tools a {
        display: block;
        padding: 1px 0 2px 25px;
        background: url(/icons/folder.png) 0 0 no-repeat;
    }
    ul.files a {
        display: block;
        padding: 1px 0 2px 25px;
        background: url(/icons/generic.png) 0 0 no-repeat;
    }
    ul.aliases a {
        background: url(/icons/forward.png) 0 0 no-repeat;
    }
    dl {
        margin: 0;
        padding: 0;
    }
    dt {
        font-weight: bold;
        text-align: right;
        width: 11em;
        clear: both;
    }
    dd {
        margin: -1.35em 0 0 12em;
        padding-bottom: 0.4em;
        overflow: auto;
    }
    dd ul li {
        float: left;
        display: block;
        width: 16.5%;
        margin: 0;
        padding: 0 0 0 20px;
        background: url(/icons/pngPlugin.png) 2px 50% no-repeat;
        line-height: 1.6;
    }
    a {
        color: #024378;
        font-weight: bold;
        text-decoration: none;
    }
    a:hover {
        color: #04569A;
        text-decoration: underline;
    }
    #foot {
        text-align: center;
        margin-top: 1.8em;
        border-top: 1px solid #999;
        padding-top: 1em;
        font-size: 0.85em;
    }
    </style>

</head>

<body>
    <div style="margin: 15px 0 0 0; border-bottom:1px solid #999999;">
    <h2>Directory Idex</h2>
    </div>
    <div style="width:50%; float: left; padding: 0 0 15px 10px;">
        <h2>Projects</h2>
        <ul class="projects">
        $projectContents
        </ul>
    </div>
    <div style="width:40%; float: left; padding: 0 10px 15px 10px;">
    <h2>Links</h2>
    <ul class="tools">
        <li class="php"><a href="?phpinfo=1">phpinfo()</a></li>
        <li class="phpmyadmin"><a href="/phpmyadmin/">phpMyAdmin</a></li>
    </ul>
    <h2>Files</h2>
    <ul class="files">
    $projectFileContents            
    </ul>
    </div>
    <ul style="clear: both;" id="foot">
        <li>&copy; Copyright $date</li>
    </ul>
</body>
</html>
EOPAGE;

echo $pageContents;
?>

I have slightly simplified the default index.php file supplied with WAMP, so that it works from any directory/location.

The problem I have is that the order is grouping capitals together.

Eg a, b, c, A, B, C

When I want: A, a, B, b, C, c

<?php
date_default_timezone_set('Europe/London');
$title = 'Project Directory';
$server = 'http://' . $_SERVER['SERVER_NAME'];
$date = date('Y');

//Directory / Files
$dir    = '.';
$files = scandir($dir);

$projectsListIgnore = array ('RemoteSystemsTempFiles','icons');
$projectsFileListIgnore = array ('.project');

$projectContents = '';
$projectFileContents = '';

foreach ($files as $file) {

    if (is_dir($file) && !in_array($file,$projectsListIgnore) && substr($file,0,1) != '_' && substr($file,0,1) != '.')  {       

        $projectContents .= '<li><a class="arrow external" href="'.$file.'">'.$file.'</a></li>';

    } 

    if (is_file($file) && !in_array($file,$projectsFileListIgnore) && substr($file,0,1) != '_' && substr($file,0,1) != '.') {
        $projectFileContents .= '<li><a class="arrow icon iicon external" href="'.$file.'"><em class="ii-download '. pathinfo($file, PATHINFO_EXTENSION) .'"></em>'.$file.'</a></li>';
    } 
}


$pageContents = <<< EOPAGE
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html lang="en" xml:lang="en">
<head>
    <title>{$title}</title>
    <meta http-equiv="Content-Type" content="txt/html; charset=utf-8" />
    <link href="icons/favicon.ico" rel="icon" type="image/x-icon" />

    <style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }

    html {
        background: #ddd;
    }
    body {
        margin: 1em 10%;
        padding: 1em 3em;
        font: 70%/1.4 arial, helvetica, lucida sans, sans-serif;
        border: 1px solid #999;
        background: #eee;
        position: relative;
    }
    #head {
        margin-bottom: 1.8em;
        margin-top: 1.8em;
        padding-bottom: 0em;
        border-bottom: 1px solid #999;
        letter-spacing: -500em;
        text-indent: -500em;
        height: 125px;
    }
    h2 {
        margin: 0.8em 0 0 0;
    }
    ul {
        list-style: none;
        margin: 0;
        padding: 0;
    }
    ul.projects, ul.tools, ul.files {
        list-style: none;
        line-height: 24px;
    }
    ul.aliases a, ul.projects a, ul.tools a {
        display: block;
        padding: 1px 0 2px 25px;
        background: url(/icons/folder.png) 0 0 no-repeat;
    }
    ul.files a {
        display: block;
        padding: 1px 0 2px 25px;
        background: url(/icons/generic.png) 0 0 no-repeat;
    }
    ul.aliases a {
        background: url(/icons/forward.png) 0 0 no-repeat;
    }
    dl {
        margin: 0;
        padding: 0;
    }
    dt {
        font-weight: bold;
        text-align: right;
        width: 11em;
        clear: both;
    }
    dd {
        margin: -1.35em 0 0 12em;
        padding-bottom: 0.4em;
        overflow: auto;
    }
    dd ul li {
        float: left;
        display: block;
        width: 16.5%;
        margin: 0;
        padding: 0 0 0 20px;
        background: url(/icons/pngPlugin.png) 2px 50% no-repeat;
        line-height: 1.6;
    }
    a {
        color: #024378;
        font-weight: bold;
        text-decoration: none;
    }
    a:hover {
        color: #04569A;
        text-decoration: underline;
    }
    #foot {
        text-align: center;
        margin-top: 1.8em;
        border-top: 1px solid #999;
        padding-top: 1em;
        font-size: 0.85em;
    }
    </style>

</head>

<body>
    <div style="margin: 15px 0 0 0; border-bottom:1px solid #999999;">
    <h2>Directory Idex</h2>
    </div>
    <div style="width:50%; float: left; padding: 0 0 15px 10px;">
        <h2>Projects</h2>
        <ul class="projects">
        $projectContents
        </ul>
    </div>
    <div style="width:40%; float: left; padding: 0 10px 15px 10px;">
    <h2>Links</h2>
    <ul class="tools">
        <li class="php"><a href="?phpinfo=1">phpinfo()</a></li>
        <li class="phpmyadmin"><a href="/phpmyadmin/">phpMyAdmin</a></li>
    </ul>
    <h2>Files</h2>
    <ul class="files">
    $projectFileContents            
    </ul>
    </div>
    <ul style="clear: both;" id="foot">
        <li>© Copyright $date</li>
    </ul>
</body>
</html>
EOPAGE;

echo $pageContents;
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2025-01-14 20:13:12

尝试 上的 natcasesort手册foreach 循环之前的 $files 数组。

例子:

$files = scandir($dir);
natcasesort($files);
foreach ($files as $file) {
    // do stuff
}

Try natcasesort (manual) on the $files array before the foreach loop.

Example:

$files = scandir($dir);
natcasesort($files);
foreach ($files as $file) {
    // do stuff
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文