gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:07:32 25 4
gpt4 key购买 nike

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

最佳答案

foreach 循环之前在 $files 数组上尝试 natcasesort ( manual)。

例子:

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

关于php - 使用 scandir() 和 sort() 的目录索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9353185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com