-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
64 lines (54 loc) · 2.01 KB
/
index.php
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
<?php
function get_media_from_folder($folder) {
$media = array();
if (!file_exists($folder) || !is_dir($folder)) {
return $media;
}
if ($handle = opendir($folder)) {
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'mp4', 'mov', 'avi', 'wmv', 'flv', 'ogv', 'ogg', 'webm'];
if (in_array($extension, $allowedExtensions)) {
$media[] = $file;
}
}
}
closedir($handle);
}
return $media;
}
$folder = 'ing/'; // 文件夹名称
if ($media = get_media_from_folder($folder)) {
$random_media = $media[array_rand($media)];
$media_path = $folder . '/' . $random_media;
if (!file_exists($media_path) || !is_readable($media_path)) {
echo "媒体文件不存在或不可读";
} else {
// 根据文件扩展名设置Content-Type
$extension = strtolower(pathinfo($media_path, PATHINFO_EXTENSION));
$contentType = null;
if ($extension == 'mp4') {
$contentType = 'video/mp4';
} else if (in_array($extension, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) {
$contentType = 'image/' . $extension;
}
if ($contentType) {
// 生成元数据文件
$metadata = [
'contentType' => $contentType,
'duration' => '10:00', // 视频时长,按需修改。
];
file_put_contents($folder . '/metadata.json', json_encode($metadata));
header('Content-Type: ' . $contentType);
header('Content-Disposition: inline');
header('Content-Length: ' . filesize($media_path));
readfile($media_path);
exit;
} else {
echo "不支持的媒体文件类型";
}
}
} else {
echo "没有找到媒体文件";
}