47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
require_once 'includes/db.php';
|
|
|
|
$id = $_GET['id'] ?? null;
|
|
|
|
try {
|
|
$app = null;
|
|
|
|
if ($id) {
|
|
// 按 ID 下载指定版本
|
|
$stmt = $db->prepare("SELECT * FROM apps WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$app = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
} else {
|
|
// 兼容旧版:返回最新版本
|
|
$stmt = $db->query("SELECT * FROM apps ORDER BY upload_time DESC LIMIT 1");
|
|
$app = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
if ($app && file_exists('data/apps/' . $app['filename'])) {
|
|
$file_path = 'data/apps/' . $app['filename'];
|
|
$file_size = filesize($file_path);
|
|
|
|
// 设置下载响应头
|
|
header('Content-Type: application/vnd.android.package-archive');
|
|
header('Content-Disposition: attachment; filename="' . $app['original_name'] . '"');
|
|
header('Content-Length: ' . $file_size);
|
|
header('Cache-Control: must-revalidate');
|
|
header('Pragma: public');
|
|
|
|
// 清空输出缓冲
|
|
if (ob_get_level()) {
|
|
ob_end_clean();
|
|
}
|
|
|
|
// 输出文件
|
|
readfile($file_path);
|
|
exit;
|
|
} else {
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
die('错误:APK 文件不存在');
|
|
}
|
|
} catch (Exception $e) {
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
die('错误:' . htmlspecialchars($e->getMessage()));
|
|
}
|