From 24380a1d9f4ea4222d9b977ea4ebc0d5f31095ad Mon Sep 17 00:00:00 2001 From: sunson Date: Sat, 30 Nov 2024 11:47:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 初始化项目 --- index.php | 491 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 491 insertions(+) create mode 100644 index.php diff --git a/index.php b/index.php new file mode 100644 index 0000000..632e9f5 --- /dev/null +++ b/index.php @@ -0,0 +1,491 @@ + '无法创建目录: ' . $upload_dir]); + exit; + } + } + + // 生成文件扩展名 + $file_extension = strtolower(pathinfo($name, PATHINFO_EXTENSION)); + + // 生成随机文件名 + $randomFileName = generateRandomFileName() . '.' . $file_extension; + + // 使用相对路径获取目标文件 + $target_file = $upload_dir . '/' . $randomFileName; + + // 检查上传错误 + if ($error !== UPLOAD_ERR_OK) { + switch ($error) { + case UPLOAD_ERR_INI_SIZE: + case UPLOAD_ERR_FORM_SIZE: + echo json_encode(['error' => '文件过大,最大允许上传:' . $max_upload_size]); + exit; + + case UPLOAD_ERR_PARTIAL: + echo json_encode(['error' => '文件部分上传失败']); + exit; + + case UPLOAD_ERR_NO_FILE: + echo json_encode(['error' => '没有文件被上传']); + exit; + + case UPLOAD_ERR_CANT_WRITE: + echo json_encode(['error' => '写入失败,无法保存文件']); + exit; + + case UPLOAD_ERR_EXTENSION: + echo json_encode(['error' => '文件上传被扩展程序阻止']); + exit; + + default: + echo json_encode(['error' => '上传失败,错误代码:' . $error]); + exit; + } + } + + // 检查硬盘空间 + if (disk_free_space($upload_dir) < filesize($file_tmp)) { + echo json_encode(['error' => '硬盘空间不足,请清理磁盘后再进行操作']); + exit; + } + + // 移动文件 + if (move_uploaded_file($file_tmp, $target_file)) { + $domain = $_SERVER['HTTP_HOST']; + $relative_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $upload_dir); + $download_url = "http://$domain" . rtrim($relative_path, '/') . '/' . rawurlencode($randomFileName); + echo json_encode(['upload_url' => htmlspecialchars($download_url)]); + } else { + echo json_encode(['error' => '写入失败,无法保存文件']); + } + exit; +} +?> + + + + + + + + 一个简单的图床 + + + +
+
+
+

一个简单的图床

+
+
+ + +
+ +
+
+
+ +
支持格式: JPEG, PNG, GIF, WEBP, BMP, SVG
+
+
+
+ + + + \ No newline at end of file