php模拟服务器实现autoindex效果的方法

瞧,那夕阳边的云霞,好似得到了夕阳的赏赐,变得欣喜异常,时而围坐一团,倾诉衷肠;时而围着夕阳跳起了探戈;更有自我陶醉的,远离他人,自我欣赏……

本文实例讲述了php模拟服务器实现autoindex效果的方法。分享给大家供大家参考。具体实现方法如下:

1.PHP代码如下:

<?php
//文件浏览程序
error_reporting(0);
$pwd = empty($_GET['dir']) ? './' : $_GET['dir'];
$pwd = realpath($pwd);
if(is_file($pwd)) {
highlight_file ($pwd);
exit;
}else
$it = new FilesystemIterator($pwd);
?>
<html>
<head>
<title>pwd of <?php echo $pwd ?></title>
</head>
<body bgcolor="white">
<h1>pwd of <?php echo $pwd ?></h1><hr>
<pre><a rel="nofollow noopener noreferrer" href="?dir=<?php echo dirname($pwd)?>">../</a>
<?php
foreach ($it as $file){
if($file->isDir()) {
$fileSize = '_';
$fileName = $file->getFilename() . '/';
} elseif($file->isFile()) {
$fileSize = $file->getSize();
$fileName = $file->getFilename();
}
$date = date('Y-m-d H:i',$file->getCTime());
?><a rel="nofollow noopener noreferrer" href="?dir=<?php echo $file->getRealPath()?>"><?php echo $fileName ?></a><?php echo str_pad($date, 60-strlen($fileName),' ',STR_PAD_LEFT)?><?php echo str_pad($fileSize,30,' ',STR_PAD_LEFT)?>
<?php }?></pre><hr>
</body>
</html>

2. 运行效果如下图所示:

希望本文所述对大家的php程序设计有所帮助。

本文php模拟服务器实现autoindex效果的方法到此结束。人,一简单就欢乐,但欢乐的人寥寥无几;一复杂就痛苦,可痛苦的人却熙熙攘攘。小编再次感谢大家对我们的支持!

标签: php autoindex