如何编写PHP脚本自动生成文件夹内文件的可下载网页链接?
Hey there! I’ve built a straightforward PHP script that’ll display all files in your target folder on a webpage, complete with working download links. It’s easy to tweak to fit your site’s needs—here’s how it works:
完整PHP脚本实现
This script includes basic styling, file metadata display, and safety checks to avoid common issues:
<?php // 配置:指定要展示文件的文件夹路径(支持相对/绝对路径) $targetFolder = './downloads'; // 示例:当前目录下的downloads文件夹 // 检查文件夹是否存在且可访问 if (!is_dir($targetFolder) || !is_readable($targetFolder)) { echo "<p>Error: 指定的文件夹不存在或无法访问,请检查路径设置。</p>"; exit; } // 读取文件夹内的文件/目录列表 $files = scandir($targetFolder); ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>文件下载中心</title> <style> .file-container { max-width: 900px; margin: 3rem auto; font-family: 'Arial', sans-serif; } .file-item { display: flex; justify-content: space-between; align-items: center; padding: 1.2rem; border-bottom: 1px solid #e0e0e0; transition: background-color 0.2s; } .file-item:hover { background-color: #f8f9fa; } .file-details { flex-grow: 1; } .file-name { font-size: 1.15rem; color: #2d3748; margin-bottom: 0.4rem; } .file-meta { font-size: 0.9rem; color: #718096; } .download-btn { background-color: #3182ce; color: white; padding: 0.6rem 1.2rem; text-decoration: none; border-radius: 4px; } .download-btn:hover { background-color: #2b6cb0; } </style> </head> <body> <div class="file-container"> <h1>可下载文件列表</h1> <?php if (empty(array_diff($files, ['.', '..']))): ?> <p>当前文件夹中暂无可下载文件。</p> <?php else: ?> <?php foreach ($files as $file): ?> <?php // 跳过系统默认的目录标识 if ($file === '.' || $file === '..') continue; $fileFullPath = $targetFolder . '/' . $file; // 跳过子文件夹(仅展示文件) if (is_dir($fileFullPath)) continue; // 获取文件元数据 $fileSize = filesize($fileFullPath); $modifiedTime = date('Y-m-d H:i:s', filemtime($fileFullPath)); // 格式化文件大小为易读格式 function formatSize($bytes) { if ($bytes >= 1048576) return number_format($bytes / 1048576, 2) . ' MB'; if ($bytes >= 1024) return number_format($bytes / 1024, 2) . ' KB'; return $bytes . ' B'; } ?> <div class="file-item"> <div class="file-details"> <div class="file-name"><?php echo htmlspecialchars($file); ?></div> <div class="file-meta"> 大小:<?php echo formatSize($fileSize); ?> | 最后修改:<?php echo $modifiedTime; ?> </div> </div> <!-- 处理中文/特殊字符文件名,确保链接有效 --> <a href="<?php echo rawurlencode($fileFullPath); ?>" class="download-btn" download>立即下载</a> </div> <?php endforeach; ?> <?php endif; ?> </div> </body> </html>
关键功能说明
- 路径配置:修改
$targetFolder为你的实际文件存储路径(比如./docs或/var/www/site/files)。 - 安全检查:先验证文件夹的合法性,避免因路径错误导致的报错。
- 文件过滤:自动跳过系统目录标识(
./..)和子文件夹,只展示可下载文件。 - 友好展示:显示文件大小(自动转换为KB/MB)和最后修改时间,提升用户体验。
- 编码处理:用
rawurlencode处理文件路径,解决中文或特殊字符导致的链接失效问题。
自定义扩展建议
- 包含子文件夹:如果需要递归展示子文件夹内的文件,替换文件遍历逻辑为递归函数:
function scanRecursive($dir) { $result = []; foreach(scandir($dir) as $item) { if ($item === '.' || $item === '..') continue; $path = $dir . '/' . $item; is_dir($path) ? $result = array_merge($result, scanRecursive($path)) : $result[] = $path; } return $result; } // 使用递归函数获取所有文件 $allFiles = scanRecursive($targetFolder);
- 限制文件类型:只允许展示特定格式的文件(如PDF/ZIP),添加以下判断:
$allowedTypes = ['pdf', 'zip', 'docx']; $fileExt = strtolower(pathinfo($fileFullPath, PATHINFO_EXTENSION)); if (!in_array($fileExt, $allowedTypes)) continue;
- 增强安全:防止目录遍历攻击,添加路径验证:
$realTarget = realpath($targetFolder); $realFile = realpath($fileFullPath); if (strpos($realFile, $realTarget) !== 0) continue;
注意事项
- 文件夹权限:确保PHP进程有读取目标文件夹的权限(通常设置为
755即可)。 - 浏览器行为:
download属性会强制浏览器触发下载,若希望浏览器直接预览文件(如图片/HTML),可移除该属性。
内容的提问来源于stack exchange,提问作者Anik Hasan
相关产品推荐
相关产品推荐

