使用scandir()和sort()的目录索引

时间:2012-02-19 20:59:53

标签: php iterator case-sensitive

我稍微简化了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;
?>

1 个答案:

答案 0 :(得分:8)

natcasesort循环之前,在$files数组上尝试foreachmanual)。

示例:

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