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

yipeiwu_com5年前服务器

本文实例讲述了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 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 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程序设计有所帮助。

相关文章

在Mac OS上使用mod_wsgi连接Python与Apache服务器

一、安装mod_wsgi 3.4: ./configure --with-apxs=/Users/levin/dev/apache2.2.27/bin/apxs --with-pyt...

使用 Python 快速实现 HTTP 和 FTP 服务器的方法

有时你需临时搭建一个简单的 Web Server,但你又不想去安装 Apache、Nginx 等这类功能较复杂的 HTTP 服务程序时。这时可以使用 Python 内建的 SimpleH...

Linux服务器下PHPMailer发送邮件失败的问题解决

Linux服务器下PHPMailer发送邮件失败的问题解决

需求 更换服务器之后,我发现我的发送邮件功能失效了!原来的服务器是可以的,一定是哪里出问题了,决定来排查一下。我是用的PHPMailer,SMTP方式发送邮件的。 排查过程 这种方式首先...

python创建一个最简单http webserver服务器的方法

本文实例讲述了python创建一个最简单http webserver服务器的方法。分享给大家供大家参考。具体实现方法如下: import sys import BaseHTTPSer...

python实现数据库跨服务器迁移

 基于Python2.7的版本环境,Python实现的数据库跨服务器(跨库)迁移, 每以5000条一查询一提交,代码中可以自行更改每次查询提交数目. # -*- codin...