php模擬服務(wù)器實(shí)現(xiàn)autoindex效果的方法

字號(hào):


    這篇文章主要介紹了php模擬服務(wù)器實(shí)現(xiàn)autoindex效果的方法,實(shí)例分析了php操作URL及傳遞參數(shù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    本文實(shí)例講述了php模擬服務(wù)器實(shí)現(xiàn)autoindex效果的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
    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. 運(yùn)行效果如下圖所示:
    名單
    名單1
    php模擬服務(wù)器實(shí)現(xiàn)autoindex效果的方法
    希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。