时区设置
date_default_timezone_set('Asia/Shanghai');
将脚本时区设置为北京时间(东八区),确保所有时间相关函数返回中国时区的时间37。
安全配置
set_time_limit(1000);
$allowed_extensions = ['pdf', 'jpg', 'jpeg', 'png'];
设置脚本最大执行时间为1000秒
定义允许处理的文件扩展名白名单
路径定义
$dataFilePath = __DIR__.'/data/files2.json';
$pdfBasePath = __DIR__.'/pdf2/';
指定JSON输出文件路径
设置PDF文件存储根目录
目录创建
if (!file_exists(dirname($dataFilePath))) {
mkdir(dirname($dataFilePath), 0755, true);
}
递归创建JSON文件所需的目录结构(如果不存在)
核心扫描函数
function scanFiles($dir, &$result, $rootDir, $allowed) {
// 扫描目录
$files = scandir($dir);
foreach ($files as $file) {
// 跳过特殊目录
if ($file === '.' || $file === '..') continue;
$fullPath = $dir.'/'.$file;
// 递归处理子目录
if (is_dir($fullPath)) {
scanFiles($fullPath, $result, $rootDir, $allowed);
continue;
}
// 文件名编码转换
$fileNameUTF8 = iconv('GBK', 'UTF-8//IGNORE', $file);
$ext = strtolower(pathinfo($fileNameUTF8, PATHINFO_EXTENSION));
// 扩展名检查
if (!in_array($ext, $allowed)) continue;
// 路径处理
$relativePath = substr($fullPath, strlen($rootDir) + 1);
$relativePathUTF8 = iconv('GBK', 'UTF-8//IGNORE', $relativePath);
// 构建文件信息数组
$result[] = [
'name' => $fileNameUTF8,
'path' => str_replace('\\', '/', $relativePathUTF8),
'size' => filesize($fullPath),
'time' => date('Y-m-d H:i:s', filemtime($fullPath))
];
}
}
路径验证
$pdfPathGBK = iconv('UTF-8', 'GBK', $pdfBasePath);
$realPdfPath = realpath($pdfPathGBK);
if (!$realPdfPath || !is_dir($realPdfPath)) {
die("PDF目录不存在或无法访问");
}
处理中文路径编码问题
验证PDF目录有效性
执行扫描
$fileList = [];
scanFiles($realPdfPath, $fileList, $realPdfPath, $allowed_extensions);
初始化空数组并开始递归扫描
排序处理
usort($fileList, function($a, $b) {
return $b['time'] - $a['time'];
});
按文件修改时间降序排序
结果输出
file_put_contents(
$dataFilePath,
json_encode(['files' => $fileList], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
);
将结果以JSON格式写入文件,保留Unicode字符和斜杠
完成提示
echo "<script>alert('已生成 ".count($fileList)." 个文件索引');</script>";
通过JavaScript弹窗显示处理结果
这段代码主要实现了:
递归扫描指定目录下的文件
过滤指定扩展名的文件
处理中文路径编码问题
生成包含文件信息的JSON索引
按修改时间排序输出结果
特别注意:
代码中多处使用iconv()处理GBK/UTF-8编码转换,说明目标环境可能存在中文Windows服务器
时区设置确保所有时间戳都显示为北京时间37
路径处理中统一使用正斜杠提高跨平台兼容性
<?php
// generate_json.php
// 设置时区为北京时间
date_default_timezone_set('Asia/Shanghai');
// 安全配置
set_time_limit(1000); // 将最大执行时间设置为60秒
$allowed_extensions = ['pdf', 'jpg', 'jpeg', 'png'];
$dataFilePath = __DIR__.'/data/files2.json'; // JSON存储路径
$pdfBasePath = __DIR__.'/pdf2/'; // PDF存储根目录
// 创建数据目录
if (!file_exists(dirname($dataFilePath))) {
mkdir(dirname($dataFilePath), 0755, true);
}
// 递归扫描目录
function scanFiles($dir, &$result, $rootDir, $allowed) {
$files = scandir($dir);
foreach ($files as $file) {
if ($file === '.' || $file === '..') continue;
$fullPath = $dir.'/'.$file;
if (is_dir($fullPath)) {
scanFiles($fullPath, $result, $rootDir, $allowed);
continue;
}
// 处理文件名编码(GBK转UTF-8)
$fileNameUTF8 = iconv('GBK', 'UTF-8//IGNORE', $file);
$ext = strtolower(pathinfo($fileNameUTF8, PATHINFO_EXTENSION));
if (!in_array($ext, $allowed)) continue;
// 计算相对路径
$relativePath = substr($fullPath, strlen($rootDir) + 1);
$relativePathUTF8 = iconv('GBK', 'UTF-8//IGNORE', $relativePath);
$result[] = [
'name' => $fileNameUTF8,
'path' => str_replace('\\', '/', $relativePathUTF8), // 统一斜杠方向
'size' => filesize($fullPath),
'time' => date('Y-m-d H:i:s', filemtime($fullPath))
];
}
}
// 验证PDF目录有效性
$pdfPathGBK = iconv('UTF-8', 'GBK', $pdfBasePath);
$realPdfPath = realpath($pdfPathGBK);
if (!$realPdfPath || !is_dir($realPdfPath)) {
die("PDF目录不存在或无法访问");
}
$fileList = [];
scanFiles($realPdfPath, $fileList, $realPdfPath, $allowed_extensions);
// 按修改时间排序
usort($fileList, function($a, $b) {
return $b['time'] - $a['time'];
});
// 写入JSON文件
file_put_contents(
$dataFilePath,
json_encode(['files' => $fileList], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
);
//echo "已生成 ".count($fileList)." 个文件索引";
echo "<script>alert('已生成 ".count($fileList)." 个文件索引');</script>";
?>
评论 (0)