PHP 简易文件下载页

使用PHP做一个超简单的文件下载页。

会自动检测除本目录下的除PHP文件外的所有文件,提供下载,TXT文本文件直接打开查看。

效果如下:

代码如下:

<?php

// 用户列表
$user_list = ['admin' => 'admin'];

$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];

if ((@$user_list[$user] ?: '') !== $pass)
{
    header('WWW-Authenticate: Basic realm="用户登录"');
    header('HTTP/1.0 401 Unauthorized');
    die("未登录");
}

$list = [];
foreach (glob('./*.*') as $r)
{
    $f = pathinfo($r);
    if ($f['extension'] == 'php')
    {
        continue;
    }

    $f['size'] = filesize($f['basename']);
    $f['time'] = date('Y-m-d H:i:s', filemtime($f['basename']));
    $list[] = $f;
}

function byte_format($size, $dec=2){
    $a = array("B", "KB", "MB", "GB", "TB", "PB");
    $pos = 0;
    while ($size >= 1024) {
         $size /= 1024;
           $pos++;
    }
    return round($size,$dec)." ".$a[$pos];
 }

?>

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>软件下载</title>
    <style>
        /* 简易表格 */
        table.tb {
            font-family: Consolas,verdana,arial,sans-serif;
            font-size:14px;
            color:#333333;
            border-collapse: collapse;
            border: solid 2px #666666;
        }
        table.tb th {
            border: solid 1px #666666;
            padding: 5px 10px;
            background-color: #dedede;
            text-align: center;
        }
        table.tb td {
            border: solid 1px #666666;
            padding: 5px 10px;
            background-color: #ffffff;
            text-align:center;
        }
    </style>

    <link href="https://lib.baomitu.com/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://lib.baomitu.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

    <script src="https://lib.baomitu.com/jquery/3.3.1/jquery.min.js"></script>
</head>
<body style="margin:10px;">
<h1>工具下载</h1>
<hr>
<table class="tb">
    <thead>
        <tr>
            <th style="text-align: left;">文件名</th>
            <th style="text-align: right;">大小</th>
            <th>修改时间</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($list as $r){ ?>
        <tr>
        <td style="text-align: left;">
            <?php if ($r['extension'] == 'txt'){ ?>
            <a target="_blank" href="<?= $r['basename'] ?>"><?= $r['basename'] ?></a>
            <?php } else { ?>
            <a download="<?= $r['basename'] ?>" href="<?= $r['basename'] ?>"><?= $r['basename'] ?></a>
            <?php } ?>
        </td>
        <td style="text-align: right;">
            <?php if ($r['size'] < 1024) { ?>
            <?= $r['size'] ?> b
            <?php }else{ ?>
            <?= byte_format($r['size']) ?>
            <?php } ?>
        </td>
        <td>
            <?= $r['time'] ?>
        </td>
        </tr>
    <?php } ?>
    </tbody>
</table>

</body>
</html>
原文地址:https://www.cnblogs.com/zjfree/p/13098864.html