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程序设计有所帮助。

相关文章

python 请求服务器的实现代码(http请求和https请求)

一、http请求 1、http请求方式:get和post get一般用于获取/查询资源信息,在浏览器中直接输入url+请求参数点击enter之后连接成功服务器就能获取到的内容,post请...

详解用Python实现自动化监控远程服务器

详解用Python实现自动化监控远程服务器

最近发现Python课器做很多事情,在监控服务器有其独特的优势,耗费资源少,开发周期短。 首先我们做一个定时或者实时脚本timedtask.py,让其定时监控目标服务器,两种方式: 第一...

python批量同步web服务器代码核心程序

#!/usr/bin/env python #coding:utf8 import os,sys import md5,tab from mysql_co.my_db impor...

开启CURL扩展,让服务器支持PHP curl函数(远程采集)

curl()、file_get_contents()、snoopy.class.php这三个远程页面抓取或采集中用到的工具,默迹还是侵向于用snoopy.class.php,因为他效率比...

PHP实现服务器状态监控的方法

本文实例讲述了PHP实现服务器状态监控的方法。分享给大家供大家参考。具体分析如下: PHP服务器状态监控对于很多朋友来讲都没做,只有看到网站挂了才知道,这种半夜网站关了是不知道情况了,对...