当前位置: 首页 > news >正文

PHP代码-服务器下载文件页面编写

内部环境的服务资源下载页面有访问需求,给开发和产品人员编写一个简洁的下载页面提供资源下载。直接用nginx+php的形式去编写了,这里提供展示index.php文件代码如下:

<?php
// 配置常量
define('BASE_DIR', __DIR__); // 当前脚本所在目录作为根目录
define('ALLOWED_DIR', realpath(BASE_DIR)); // 安全校验路径// 处理文件下载
if (isset($_GET['download'])) {$filePath = realpath($_GET['download']);if ($filePath && strpos($filePath, ALLOWED_DIR) === 0 && is_file($filePath)) {header('Content-Type: application/octet-stream');header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');header('Content-Length: ' . filesize($filePath));readfile($filePath);exit;} else {die('<div class="p-4 text-red-500">文件不存在或禁止访问</div>');}
}// 获取当前安全目录路径
$currentDir = isset($_GET['dir']) ? realpath($_GET['dir']) : ALLOWED_DIR;
if (!$currentDir || strpos($currentDir, ALLOWED_DIR) !== 0) {$currentDir = ALLOWED_DIR; // 非法路径重置为根目录
}// 获取文件列表
$files = scandir($currentDir);
$fileList = [];
foreach ($files as $file) {if ($file == '.' || $file == '..') continue;$fullPath = $currentDir . DIRECTORY_SEPARATOR . $file;$fileList[] = ['name' => $file,'type' => is_dir($fullPath) ? 'directory' : 'file','size' => is_file($fullPath) ? formatSize(filesize($fullPath)) : '-','mtime' => date('Y-m-d H:i:s', filemtime($fullPath)),'path' => $fullPath];
}// 辅助函数:格式化文件大小
function formatSize($bytes) {$units = ['B', 'KB', 'MB', 'GB'];$index = 0;while ($bytes >= 1024 && $index < 3) {$bytes /= 1024;$index++;}return round($bytes, 2) . ' ' . $units[$index];
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>文件管理系统</title><script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="p-8 bg-gray-50"><div class="max-w-4xl mx-auto bg-white rounded-lg shadow-md p-6"><h1 class="text-2xl font-bold text-gray-800 mb-6">文件列表: <?= htmlspecialchars(str_replace(ALLOWED_DIR, '/', $currentDir)) ?></h1><!-- 面包屑导航(仅在非根目录显示) --><nav class="mb-4 text-sm text-gray-600"><?php$parentDir = dirname($currentDir);if ($currentDir !== ALLOWED_DIR && $parentDir != $currentDir && strpos(realpath($parentDir), ALLOWED_DIR) === 0):?><a href="?dir=<?= urlencode($parentDir) ?>" class="text-blue-500 hover:text-blue-700">⬆ 上一级</a><?php endif; ?></nav><!-- 文件表格 --><table class="w-full border-collapse"><thead class="bg-gray-50"><tr><th class="p-3 text-left text-gray-500">名称</th><th class="p-3 text-left text-gray-500">类型</th><th class="p-3 text-left text-gray-500">大小</th><th class="p-3 text-left text-gray-500">修改时间</th><th class="p-3 text-left text-gray-500">操作</th></tr></thead><tbody><?php foreach ($fileList as $item): ?><tr class="hover:bg-gray-50 border-t"><td class="p-3"><?php if ($item['type'] == 'directory'): ?><a href="?dir=<?= urlencode($item['path']) ?>" class="text-blue-500 hover:text-blue-700 flex items-center">📁 <?= htmlspecialchars($item['name']) ?></a><?php else: ?><span class="flex items-center">📄 <?= htmlspecialchars($item['name']) ?></span><?php endif; ?></td><td class="p-3 text-gray-600"><?= $item['type'] == 'directory' ? '文件夹' : '文件' ?></td><td class="p-3 text-gray-600"><?= $item['size'] ?></td><td class="p-3 text-gray-600"><?= $item['mtime'] ?></td><td class="p-3"><?php if ($item['type'] == 'file'): ?><a href="?download=<?= urlencode($item['path']) ?>" class="px-3 py-1 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors">下载</a><?php else: ?><span class="text-gray-400">-</span><?php endif; ?></td></tr><?php endforeach; ?></tbody></table></div>
</body>
</html>

最终的页面展示:

相关文章:

  • tarjan缩点+强联通分量
  • 数据库day-08
  • 医药生产的“神助攻”:疆鸿智能JH-ECT012,EtherCAT转CC LINK IE超靠谱
  • 如何有效保护iOS应用?分享一款实用的IPA混淆工具
  • Java高频面试之并发编程-10
  • 【无需安装额外软件,JavaScript脚本】B站批量取消关注
  • Consul安装部署(Windows环境)
  • 为什么要学习《易经》?
  • ESP32- 开发笔记- 软件开发 4 - GPIO 口
  • 学习基本宠物美容
  • 使用org.java_websocket库第三方库实现广播
  • RISC-V架构低功耗MCU中断控制系统设计
  • 《Vue3学习手记7》
  • 3D Gaussian Splatting部分原理介绍和CUDA代码解读
  • grafana/loki 设置日志保留时间
  • C++的vector中emplace_back() 与 push_back() 的区别
  • 高防 CDN 的防护功能有哪些?从基础防护到智能防御的全面解析
  • redis高级进阶
  • 记录 Flink jdbc、mysql-cdc 连接 mysql8 碰到的适配问题
  • Leetcode刷题记录22——滑动窗口最大值
  • 石家庄:城市更新,生活向美
  • 新剧|反谍大剧《绝密较量》央一开播,张鲁一高圆圆主演
  • 报告:到2030年我国无人机产业将率先实现万亿规模
  • 美国政府将暂时恢复部分受影响留学生的合法身份,并将制订新标准
  • 酒店保洁员调包住客港币,海南官方通报:成立调查组赴属地调查
  • 建投读书会·东西汇流|全球物品:跨文化交流视域下的明清外销瓷