dongrong5189 2012-02-19 20:59
浏览 100
已采纳

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

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>&copy; Copyright $date</li>
    </ul>
</body>
</html>
EOPAGE;

echo $pageContents;
?>
  • 写回答

1条回答 默认 最新

  • doucaishi0077 2012-02-19 21:09
    关注

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

    Example:

    $files = scandir($dir);
    natcasesort($files);
    foreach ($files as $file) {
        // do stuff
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。