xiaozhian commited on
Commit
dc45f00
·
verified ·
1 Parent(s): f81eb5d

Upload 10 files

Browse files
Files changed (10) hide show
  1. .gitignore +4 -0
  2. Dockerfile +62 -0
  3. delete.php +40 -0
  4. docker-compose.yml +9 -0
  5. images.php +25 -0
  6. img.php +45 -0
  7. index.html +336 -0
  8. router.php +59 -0
  9. upload.php +70 -0
  10. videos.php +44 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ img/*
2
+ videos/*
3
+ !img/.gitkeep
4
+ !videos/.gitkeep
Dockerfile ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM ubuntu:22.04
2
+
3
+ # 设置非交互模式和时区
4
+ ENV DEBIAN_FRONTEND=noninteractive
5
+ ENV TZ=Asia/Shanghai
6
+
7
+ # 更新包列表并安装基础依赖
8
+ RUN apt-get update && apt-get install -y \
9
+ software-properties-common \
10
+ curl \
11
+ wget \
12
+ git \
13
+ && rm -rf /var/lib/apt/lists/*
14
+
15
+ # 添加 PHP 仓库并安装 PHP
16
+ RUN add-apt-repository ppa:ondrej/php && \
17
+ apt-get update && \
18
+ apt-get install -y \
19
+ php8.3-cli \
20
+ php8.3-fpm \
21
+ php8.3-common \
22
+ php8.3-xml \
23
+ php8.3-curl \
24
+ php8.3-mbstring \
25
+ && rm -rf /var/lib/apt/lists/*
26
+
27
+ # 设置工作目录
28
+ WORKDIR /var/www/html
29
+
30
+ # 复制项目文件
31
+ COPY . /var/www/html/
32
+
33
+ # 创建必要的目录
34
+ RUN mkdir -p /var/www/html/img \
35
+ /var/www/html/videos \
36
+ && chown -R www-data:www-data /var/www/html \
37
+ && find /var/www/html -type d -exec chmod 755 {} \; \
38
+ && find /var/www/html -type f -exec chmod 644 {} \; \
39
+ && chmod -R 777 /var/www/html/img \
40
+ && chmod -R 777 /var/www/html/videos
41
+
42
+ # 配置 PHP
43
+ RUN echo "display_errors = On" >> /etc/php/8.3/cli/conf.d/error-reporting.ini && \
44
+ echo "error_reporting = E_ALL" >> /etc/php/8.3/cli/conf.d/error-reporting.ini && \
45
+ echo "log_errors = On" >> /etc/php/8.3/cli/conf.d/error-reporting.ini && \
46
+ echo "error_log = /dev/stderr" >> /etc/php/8.3/cli/conf.d/error-reporting.ini && \
47
+ echo "upload_max_filesize = 100M" >> /etc/php/8.3/cli/conf.d/uploads.ini && \
48
+ echo "post_max_size = 100M" >> /etc/php/8.3/cli/conf.d/uploads.ini
49
+
50
+ # 创建启动脚本
51
+ RUN echo '#!/bin/bash\n\
52
+ exec php -S 0.0.0.0:7860 -t /var/www/html /var/www/html/router.php 2>&1 | grep -v "Accepted\|Closing\|preconnection"' > /usr/local/bin/docker-entrypoint.sh \
53
+ && chmod +x /usr/local/bin/docker-entrypoint.sh
54
+
55
+ # 暴露端口
56
+ EXPOSE 7860
57
+
58
+ # 切换到 www-data 用户
59
+ USER www-data
60
+
61
+ # 启动 PHP 内置服务器
62
+ CMD ["/usr/local/bin/docker-entrypoint.sh"]
delete.php ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $response = ["code" => 400, "msg" => "failed"];
3
+
4
+ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
5
+ $data = json_decode(file_get_contents('php://input'), true);
6
+ if (isset($data['filename'])) {
7
+ $filename = basename($data['filename']); // Ensure the filename is safe
8
+
9
+ // Define file paths
10
+ $imagePath = 'img/' . $filename;
11
+ $videoPath = 'videos/' . $filename;
12
+
13
+ // Check if the file exists in the image directory
14
+ if (file_exists($imagePath)) {
15
+ if (unlink($imagePath)) {
16
+ $response['code'] = 200;
17
+ $response['msg'] = 'Image deleted successfully';
18
+ } else {
19
+ $response['msg'] = 'Failed to delete image';
20
+ }
21
+ }
22
+ // Check if the file exists in the video directory
23
+ elseif (file_exists($videoPath)) {
24
+ if (unlink($videoPath)) {
25
+ $response['code'] = 200;
26
+ $response['msg'] = 'Video deleted successfully';
27
+ } else {
28
+ $response['msg'] = 'Failed to delete video';
29
+ }
30
+ }
31
+ else {
32
+ $response['msg'] = 'File not found';
33
+ }
34
+ } else {
35
+ $response['msg'] = 'Filename not provided';
36
+ }
37
+ }
38
+
39
+ echo json_encode($response);
40
+ ?>
docker-compose.yml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ version: '3'
2
+ services:
3
+ web:
4
+ build: .
5
+ ports:
6
+ - "7860:7860"
7
+ volumes:
8
+ - ./img:/var/www/html/img
9
+ - ./videos:/var/www/html/videos
images.php ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $imageDir = 'img/';
3
+ $videoDir = 'videos/';
4
+ $images = [];
5
+ $videos = [];
6
+
7
+ // 获取图片文件列表
8
+ if (is_dir($imageDir)) {
9
+ $imageFiles = array_diff(scandir($imageDir), ['..', '.']);
10
+ foreach ($imageFiles as $file) {
11
+ $images[] = $imageDir . $file;
12
+ }
13
+ }
14
+
15
+ // 获取视频文件列表
16
+ if (is_dir($videoDir)) {
17
+ $videoFiles = array_diff(scandir($videoDir), ['..', '.']);
18
+ foreach ($videoFiles as $file) {
19
+ $videos[] = $videoDir . $file;
20
+ }
21
+ }
22
+
23
+ // 返回JSON响应
24
+ echo json_encode(['images' => $images, 'videos' => $videos]);
25
+ ?>
img.php ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $directory = 'img';
3
+ $images = [];
4
+
5
+ // 支持的图片扩展名
6
+ $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'tiff', 'svg'];
7
+
8
+ // 打开目录并读取图片文件
9
+ if (is_dir($directory)) {
10
+ if ($dh = opendir($directory)) {
11
+ while (($file = readdir($dh)) !== false) {
12
+ $filePath = $directory . '/' . $file;
13
+ // 获取文件扩展名并检查是否为图片
14
+ $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
15
+ if (is_file($filePath) && in_array($fileExtension, $imageExtensions)) {
16
+ $images[] = $file;
17
+ }
18
+ }
19
+ closedir($dh);
20
+ }
21
+ }
22
+
23
+ // 随机选择一张图片
24
+ if (!empty($images)) {
25
+ $image = $images[array_rand($images)];
26
+ $imagePath = $directory . '/' . $image;
27
+
28
+ // 获取图片的MIME类型
29
+ $imageInfo = getimagesize($imagePath);
30
+ $mimeType = $imageInfo['mime'];
31
+
32
+ // 设置HTTP头部
33
+ header('Content-Type: ' . $mimeType);
34
+ header('Content-Length: ' . filesize($imagePath));
35
+
36
+ // 输出图片内容
37
+ readfile($imagePath);
38
+ exit;
39
+ } else {
40
+ // 如果没有找到图片,则返回404错误
41
+ header("HTTP/1.0 404 Not Found");
42
+ echo 'No images found in the directory.';
43
+ exit;
44
+ }
45
+ ?>
index.html ADDED
@@ -0,0 +1,336 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Responsive Waterfall Media Gallery</title>
7
+ <style>
8
+ body {
9
+ font-family: Arial, sans-serif;
10
+ display: flex;
11
+ justify-content: center;
12
+ align-items: center;
13
+ flex-direction: column;
14
+ background: url('https://zhian.serv00.net/img/Image_1722541553.png') no-repeat center center fixed;
15
+ background-size: cover;
16
+ margin: 0;
17
+ padding: 0;
18
+ min-height: 100vh;
19
+ }
20
+
21
+ .upload-container {
22
+ margin-top: 20px;
23
+ text-align: center;
24
+ background-color: rgba(255, 255, 255, 0.8);
25
+ padding: 10px;
26
+ border-radius: 5px;
27
+ }
28
+
29
+ .gallery {
30
+ width: 90%;
31
+ background-color: rgba(255, 255, 255, 0.8);
32
+ border-radius: 5px;
33
+ padding: 20px;
34
+ box-sizing: border-box;
35
+ column-count: 4;
36
+ column-gap: 10px;
37
+ }
38
+
39
+ .gallery-item {
40
+ position: relative;
41
+ break-inside: avoid;
42
+ margin-bottom: 10px;
43
+ border-radius: 8px;
44
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
45
+ overflow: hidden;
46
+ }
47
+
48
+ .gallery-item img,
49
+ .gallery-item video {
50
+ width: 100%;
51
+ height: auto;
52
+ display: block;
53
+ object-fit: cover;
54
+ transition: opacity 0.3s ease, transform 0.3s ease;
55
+ }
56
+
57
+ .gallery img:hover, .gallery video:hover {
58
+ transform: scale(1.05);
59
+ }
60
+ .gallery img.deleting, .gallery video.deleting {
61
+ transition: transform 0.5s ease-out, opacity 0.5s ease-out;
62
+ transform: translateX(-100%);
63
+ opacity: 0;
64
+ }
65
+ .gallery img.marked, .gallery video.marked {
66
+ border: 5px solid rgba(255, 0, 0, 0.5);
67
+ }
68
+ #uploadBtn {
69
+ margin-bottom: 20px;
70
+ }
71
+
72
+ .actions {
73
+ position: absolute;
74
+ bottom: 0;
75
+ left: 0;
76
+ right: 0;
77
+ display: flex;
78
+ justify-content: space-around;
79
+ align-items: center;
80
+ width: 100%;
81
+ background: rgba(0, 0, 0, 0.5);
82
+ color: white;
83
+ text-align: center;
84
+ padding: 10px 0;
85
+ opacity: 0;
86
+ transition: opacity 0.3s ease;
87
+ }
88
+
89
+ .actions button {
90
+ flex: 1;
91
+ background: none;
92
+ border: none;
93
+ color: white;
94
+ cursor: pointer;
95
+ padding: 5px 10px;
96
+ }
97
+
98
+ .gallery-item:hover .actions {
99
+ opacity: 1;
100
+ }
101
+
102
+ .gallery-item.clicked img {
103
+ opacity: 0;
104
+ transform: scale(0.8);
105
+ transition: opacity 0.3s ease, transform 0.3s ease;
106
+ }
107
+
108
+ #progressBarContainer {
109
+ width: 80%;
110
+ margin: 20px auto;
111
+ background-color: #f3f3f3;
112
+ border: 1px solid #ccc;
113
+ border-radius: 5px;
114
+ display: none;
115
+ }
116
+
117
+ #progressBar {
118
+ width: 0;
119
+ height: 20px;
120
+ background-color: #4caf50;
121
+ border-radius: 5px;
122
+ }
123
+
124
+ .popup {
125
+ position: fixed;
126
+ top: 20px;
127
+ right: 20px;
128
+ background-color: #4caf50;
129
+ color: white;
130
+ padding: 10px;
131
+ border-radius: 5px;
132
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
133
+ z-index: 1000;
134
+ display: none;
135
+ }
136
+
137
+ @media (max-width: 1024px) {
138
+ .gallery {
139
+ column-count: 3;
140
+ }
141
+ }
142
+
143
+ @media (max-width: 768px) {
144
+ .gallery {
145
+ column-count: 2;
146
+ }
147
+ }
148
+
149
+ @media (max-width: 480px) {
150
+ .gallery {
151
+ column-count: 1;
152
+ }
153
+ }
154
+ </style>
155
+ </head>
156
+ <body>
157
+ <div class="upload-container">
158
+ <input type="file" id="uploadBtn" accept="image/*,video/*" multiple>
159
+ </div>
160
+ <div id="progressBarContainer">
161
+ <div id="progressBar"></div>
162
+ </div>
163
+ <div class="gallery" id="gallery"></div>
164
+ <div class="popup" id="popup">Upload Successful!</div>
165
+
166
+ <script>
167
+ document.addEventListener("DOMContentLoaded", function() {
168
+ const gallery = document.getElementById("gallery");
169
+ const uploadBtn = document.getElementById("uploadBtn");
170
+ const progressBarContainer = document.getElementById("progressBarContainer");
171
+ const progressBar = document.getElementById("progressBar");
172
+ const popup = document.getElementById("popup");
173
+ const longPressDuration = 500;
174
+ let touchTimer = null;
175
+
176
+ // Function to determine if the device is mobile
177
+ function isMobileDevice() {
178
+ return /Mobi|Android/i.test(navigator.userAgent);
179
+ }
180
+
181
+ uploadBtn.addEventListener("change", function(event) {
182
+ Array.from(event.target.files).forEach(file => {
183
+ const formData = new FormData();
184
+ formData.append("file", file);
185
+
186
+ const xhr = new XMLHttpRequest();
187
+ xhr.open('POST', 'upload.php', true);
188
+
189
+ xhr.upload.onprogress = function(event) {
190
+ if (event.lengthComputable) {
191
+ const percentComplete = (event.loaded / event.total) * 100;
192
+ progressBar.style.width = percentComplete + '%';
193
+ }
194
+ };
195
+
196
+ xhr.onloadstart = function() {
197
+ progressBarContainer.style.display = 'block';
198
+ };
199
+
200
+ xhr.onloadend = function() {
201
+ progressBarContainer.style.display = 'none';
202
+ progressBar.style.width = '0%';
203
+ };
204
+
205
+ xhr.onload = function() {
206
+ if (xhr.status === 200) {
207
+ const response = JSON.parse(xhr.responseText);
208
+ if (response.code === 200) {
209
+ let mediaElement;
210
+ if (response.img) {
211
+ mediaElement = createMediaElement(response.img, 'img');
212
+ } else if (response.video) {
213
+ mediaElement = createMediaElement(response.video, 'video');
214
+ }
215
+ gallery.appendChild(mediaElement);
216
+ showPopup("上传成功!");
217
+ } else {
218
+ console.error('上传失败:', response.msg);
219
+ }
220
+ } else {
221
+ console.error('上传失败, 服务器返回状态码:', xhr.status);
222
+ }
223
+ };
224
+
225
+ xhr.send(formData);
226
+ });
227
+ });
228
+
229
+ fetch('images.php')
230
+ .then(response => response.json())
231
+ .then(data => {
232
+ if (data.images && data.images.length > 0) {
233
+ data.images.forEach(src => {
234
+ const mediaElement = createMediaElement(src, 'img');
235
+ gallery.appendChild(mediaElement);
236
+ });
237
+ }
238
+ if (data.videos && data.videos.length > 0) {
239
+ data.videos.forEach(src => {
240
+ const mediaElement = createMediaElement(src, 'video');
241
+ gallery.appendChild(mediaElement);
242
+ });
243
+ }
244
+ })
245
+ .catch(error => console.error('获取图像和视频时出错:', error));
246
+
247
+ function createMediaElement(src, type) {
248
+ const mediaElement = document.createElement('div');
249
+ mediaElement.className = 'gallery-item';
250
+ let element;
251
+ if (type === 'img') {
252
+ element = document.createElement('img');
253
+ element.src = src;
254
+ element.alt = 'Image';
255
+ } else {
256
+ element = document.createElement('video');
257
+ element.src = src;
258
+ element.controls = true;
259
+ element.alt = 'Video';
260
+ }
261
+
262
+ element.oncontextmenu = function(event) {
263
+ event.preventDefault();
264
+ copyToClipboard(src);
265
+ };
266
+
267
+ element.addEventListener('touchstart', function(event) {
268
+ touchTimer = setTimeout(() => copyToClipboard(src), longPressDuration);
269
+ }, false);
270
+
271
+ element.addEventListener('touchend', function() {
272
+ if (touchTimer) clearTimeout(touchTimer);
273
+ }, false);
274
+
275
+ const actions = document.createElement('div');
276
+ actions.className = 'actions';
277
+ const copyButton = document.createElement('button');
278
+ copyButton.textContent = '复制链接';
279
+ copyButton.onclick = function() { copyToClipboard(src); };
280
+ const deleteButton = document.createElement('button');
281
+ deleteButton.className = 'delete-button';
282
+ deleteButton.textContent = '删除';
283
+ deleteButton.onclick = function() { deleteMedia(mediaElement, src); };
284
+ actions.appendChild(copyButton);
285
+ actions.appendChild(deleteButton);
286
+ mediaElement.appendChild(element);
287
+ mediaElement.appendChild(actions);
288
+ return mediaElement;
289
+ }
290
+
291
+ function copyToClipboard(src) {
292
+ const url = new URL(src, window.location.origin).href;
293
+ navigator.clipboard.writeText(url)
294
+ .then(() => {
295
+ if (!isMobileDevice()) {
296
+ alert('链接已复制到剪贴板');
297
+ }
298
+ })
299
+ .catch(err => console.error('复制失败:', err));
300
+ }
301
+
302
+ function deleteMedia(mediaElement, src) {
303
+ mediaElement.classList.add('clicked');
304
+
305
+ setTimeout(() => {
306
+ fetch('delete.php', {
307
+ method: 'POST',
308
+ headers: {
309
+ 'Content-Type': 'application/json'
310
+ },
311
+ body: JSON.stringify({ filename: src.split('/').pop() })
312
+ })
313
+ .then(response => response.json())
314
+ .then(data => {
315
+ if (data.code === 200) {
316
+ gallery.removeChild(mediaElement);
317
+ showPopup('删除成功');
318
+ } else {
319
+ console.error('删除失败:', data.msg);
320
+ }
321
+ })
322
+ .catch(error => console.error('删除媒体时出错:', error));
323
+ }, 300);
324
+ }
325
+
326
+ function showPopup(message) {
327
+ popup.textContent = message;
328
+ popup.style.display = 'block';
329
+ setTimeout(() => {
330
+ popup.style.display = 'none';
331
+ }, 2000);
332
+ }
333
+ });
334
+ </script>
335
+ </body>
336
+ </html>
router.php ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // 设置时区
3
+ date_default_timezone_set('Asia/Shanghai');
4
+
5
+ // 处理错误输出
6
+ ini_set('display_errors', 'Off');
7
+ error_reporting(E_ALL);
8
+ ini_set('log_errors', 'On');
9
+ ini_set('error_log', '/dev/stderr');
10
+
11
+ // 设置 Content-Type 为 JSON(针对 API 请求)
12
+ if (in_array(basename($_SERVER['REQUEST_URI']), [
13
+ 'delete.php',
14
+ 'images.php',
15
+ 'videos.php',
16
+ 'upload.php'
17
+ ])) {
18
+ header('Content-Type: application/json');
19
+ }
20
+
21
+ // 处理静态文件
22
+ if (preg_match('/\.(css|js|png|jpg|jpeg|gif|ico|webp|mp4)$/', $_SERVER['REQUEST_URI'])) {
23
+ return false; // 让 PHP 内置服务器处理静态文件
24
+ }
25
+
26
+ // 根据请求路径加载对应的 PHP 文件
27
+ $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
28
+ $file = ltrim($path, '/');
29
+
30
+ // API 路由映射
31
+ $apiRoutes = [
32
+ 'delete.php' => 'delete.php',
33
+ 'images.php' => 'images.php',
34
+ 'img.php' => 'img.php',
35
+ 'upload.php' => 'upload.php',
36
+ 'videos.php' => 'videos.php'
37
+ ];
38
+
39
+ // 处理 API 请求
40
+ if (isset($apiRoutes[$file])) {
41
+ require __DIR__ . '/' . $apiRoutes[$file];
42
+ return true;
43
+ }
44
+
45
+ // 如果是空路径或根路径,返回 index.html
46
+ if ($file === '' || $file === 'index.html') {
47
+ require __DIR__ . '/index.html';
48
+ return true;
49
+ }
50
+
51
+ // 如果文件存在且是 PHP 文件,则执行它
52
+ if (file_exists(__DIR__ . '/' . $file) && preg_match('/\.php$/', $file)) {
53
+ require __DIR__ . '/' . $file;
54
+ return true;
55
+ }
56
+
57
+ // 默认返回 index.html
58
+ require __DIR__ . '/index.html';
59
+ return true;
upload.php ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // 设置上传目录
3
+ $imageDir = 'img/';
4
+ $videoDir = 'videos/';
5
+
6
+ // 检查并创建目录
7
+ if (!is_dir($imageDir)) {
8
+ mkdir($imageDir, 0777, true);
9
+ }
10
+ if (!is_dir($videoDir)) {
11
+ mkdir($videoDir, 0777, true);
12
+ }
13
+
14
+ // 获取协议
15
+ $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
16
+ // 获取客户端IP和端口
17
+ $host = $_SERVER['HTTP_HOST'];
18
+ $baseUrl = $protocol . $host . dirname($_SERVER['SCRIPT_NAME']) . '/';
19
+
20
+ // 准备响应数组
21
+ $response = ["code" => 400, "msg" => "failed"];
22
+
23
+ // 检查是否有文件上传
24
+ if ($_FILES) {
25
+ // 获取文件信息
26
+ $file = $_FILES['file'];
27
+ $filename = basename($file['name']);
28
+ $fileTmpPath = $file['tmp_name'];
29
+ $fileSize = $file['size'];
30
+ $fileType = $file['type'];
31
+
32
+ // 设置文件名及上传路径
33
+ if (strpos($fileType, 'image') !== false) {
34
+ // 处理图片上传
35
+ $ext = pathinfo($filename, PATHINFO_EXTENSION);
36
+ $newFilename = 'Image_' . time() . '.' . $ext;
37
+ $uploadFilePath = $imageDir . $newFilename;
38
+ $urlPath = $baseUrl . $imageDir . $newFilename;
39
+ } elseif (strpos($fileType, 'video') !== false) {
40
+ // 处理视频上传
41
+ $ext = pathinfo($filename, PATHINFO_EXTENSION);
42
+ $newFilename = 'Video_' . time() . '.' . $ext;
43
+ $uploadFilePath = $videoDir . $newFilename;
44
+ $urlPath = $baseUrl . $videoDir . $newFilename;
45
+ } else {
46
+ // 无效的文件类型
47
+ $response['msg'] = 'Invalid file type';
48
+ echo json_encode($response);
49
+ exit;
50
+ }
51
+
52
+ // 移动文件到指定目录
53
+ if (move_uploaded_file($fileTmpPath, $uploadFilePath)) {
54
+ $response['code'] = 200;
55
+ $response['msg'] = 'success';
56
+ if (strpos($fileType, 'image') !== false) {
57
+ $response['img'] = $urlPath;
58
+ } elseif (strpos($fileType, 'video') !== false) {
59
+ $response['videos'] = $urlPath;
60
+ }
61
+ } else {
62
+ $response['msg'] = 'File upload failed';
63
+ }
64
+ } else {
65
+ $response['msg'] = 'No file uploaded';
66
+ }
67
+
68
+ // 返回JSON响应并替换\/为/
69
+ echo str_replace('\/', '/', json_encode($response));
70
+ ?>
videos.php ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // 设置Content-Type为application/json
3
+ header('Content-Type: application/json');
4
+
5
+ // 禁止缓存的HTTP头
6
+ header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1
7
+ header('Pragma: no-cache'); // HTTP 1.0
8
+ header('Expires: 0'); // Proxies
9
+
10
+ // 定义视频文件夹的路径
11
+ $videoDir = 'videos';
12
+
13
+ // 获取视频文件列表
14
+ $videos = glob($videoDir . '/*.mp4');
15
+
16
+ // 检查是否有视频文件
17
+ if ($videos === false || count($videos) === 0) {
18
+ $response = [
19
+ "code" => 500,
20
+ "msg" => "No videos found",
21
+ "videos" => null
22
+ ];
23
+ } else {
24
+ // 随机选择一个视频文件
25
+ $randomVideo = $videos[array_rand($videos)];
26
+ $videoUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $randomVideo;
27
+
28
+ // 构建响应数组
29
+ $response = [
30
+ "code" => 200,
31
+ "msg" => "success",
32
+ "videos" => $videoUrl
33
+ ];
34
+ }
35
+
36
+ // 将响应数组编码为JSON
37
+ $jsonResponse = json_encode($response);
38
+
39
+ // 替换JSON字符串中的反斜杠
40
+ $jsonResponse = str_replace('\/', '/', $jsonResponse);
41
+
42
+ // 输出JSON响应
43
+ echo $jsonResponse;
44
+ ?>