-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync_server.php
More file actions
66 lines (55 loc) · 1.77 KB
/
sync_server.php
File metadata and controls
66 lines (55 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
function getDirectoryStructure($dir) {
$result = [];
// 创建一个目录迭代器
$iterator = new DirectoryIterator($dir);
foreach ($iterator as $fileinfo) {
// 跳过 '.' 和 '..'
if ($fileinfo->isDot()) {
continue;
}
$path = $fileinfo->getPathname();
$relativePath = str_replace(__DIR__, '', $path);
if ($fileinfo->isDir()) {
// 递归处理子目录
$result[] = [
'type' => 'directory',
'path' => $relativePath,
'contents' => getDirectoryStructure($path)
];
} else {
// 处理文件
$result[] = [
'type' => 'file',
'path' => $relativePath,
'size' => $fileinfo->getSize(),
'mtime' => $fileinfo->getMTime() // 添加文件修改时间
];
}
}
return $result;
}
// 获取指定文件的字节集
function getFileBytes($filePath) {
if (file_exists($filePath)) {
return file_get_contents($filePath);
} else {
return 'File not found.';
}
}
if (isset($_GET['file'])) {
$requestedFile = $_GET['file'];
// 如果请求参数中包含文件路径
// 则返回该文件的字节集
echo getFileBytes(__DIR__ . $requestedFile);
} else {
// 否则返回整个目录结构的 JSON
$directoryStructure = getDirectoryStructure(__DIR__);
// 将结果编码为 JSON 格式
$jsonResult = json_encode($directoryStructure, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
// 将 JSON 输出到页面
echo $jsonResult;
// 如果想将 JSON 保存到文件中,可以使用下面的代码
// file_put_contents('directory_structure.json', $jsonResult);
}
?>