100 * 1024 * 1024) { log_error('应用文件过大: ' . number_format($appFile['size'] / 1024 / 1024, 2) . 'MB', __FILE__, __LINE__); $error = '应用文件大小不能超过100MB'; } $appExtension = pathinfo($appFile['name'], PATHINFO_EXTENSION); $appFileName = uniqid() . '.' . $appExtension; $appRelativePath = 'uploads/apps/' . $appFileName; $appFilePath = __DIR__ . '/../' . $appRelativePath; if (!move_uploaded_file($appFile['tmp_name'], $appFilePath)) { log_error('应用文件移动失败', __FILE__, __LINE__); $error = '应用文件上传失败'; } } else { $error = '应用文件上传错误: ' . ($appFile ? $appFile['error'] : '未找到文件'); } // 处理图片上传 $imagePaths = []; $images = $_FILES['images'] ?? null; if ($images && is_array($images['tmp_name'])) { foreach ($images['tmp_name'] as $key => $tmpName) { if ($images['error'][$key] === UPLOAD_ERR_OK) { // 验证图片大小 (10MB) if ($images['size'][$key] > 10 * 1024 * 1024) { log_error('图片过大: ' . $images['name'][$key] . ' (' . number_format($images['size'][$key] / 1024 / 1024, 2) . 'MB)', __FILE__, __LINE__); $error = '图片 ' . $images['name'][$key] . ' 大小不能超过10MB'; } $imageRelativePath = 'uploads/images/' . uniqid() . '.' . pathinfo($images['name'][$key], PATHINFO_EXTENSION); $imagePath = __DIR__ . '/../' . $imageRelativePath; $target_dir = dirname($imagePath); if (!is_dir($target_dir)) { mkdir($target_dir, 0755, true); } if (move_uploaded_file($tmpName, $imagePath)) { $imagePaths[] = $imagePath; } else { log_error('图片文件移动失败: ' . $images['name'][$key], __FILE__, __LINE__); } } } } if (empty($error)) { // 开始数据库事务 $conn->begin_transaction(); try { // 确保必要变量存在,防止空值导致 SQL 错误 if (!isset($appName) || !isset($appDescription) || !isset($developerId) || !isset($version) || !isset($changelog) || !isset($ageRating) || !isset($ageRatingDescription)) { throw new Exception('缺少必要的上传参数'); } // 插入应用基本信息 $filePath = ''; // 初始化file_path为空字符串以满足数据库要求 // 添加created_at字段并设置为当前时间戳 $stmt = $conn->prepare('INSERT INTO apps (name, description, developer_id, platforms, status, age_rating, age_rating_description, version, changelog, file_path, created_at) VALUES (?, ?, ?, ?, \'pending\', ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)'); if (!$stmt) { throw new Exception('应用基本信息查询准备失败: ' . $conn->error); } // 确保平台数据正确编码 $platforms = $_POST['platforms'] ?? []; $platforms_json = json_encode($platforms); // 此处需确认预处理语句占位符数量,确保与 bind_param 参数数量一致,示例仅示意,实际需根据表结构调整 // 修正参数绑定,添加file_path参数以匹配SQL占位符数量 // 修正参数类型字符串长度,确保与10个参数匹配 // 修正类型字符串长度,10个参数对应10个类型字符 // 最终修正:10个参数对应10个类型字符 // 根据参数实际类型修正类型字符串(整数用i,字符串用s) // 移除多余的$status参数,匹配SQL中9个占位符 // 修正age_rating_description类型为字符串,并确保9个参数与占位符匹配 // 修复变量名错误:使用已验证的$appFilePath替换未定义的$file_path $stmt->bind_param('ssissssss', $appName, $appDescription, $developerId, $platforms_json, $ageRating, $ageRatingDescription, $version, $changelog, $filePath); if (!$stmt->execute()) { throw new Exception('应用基本信息查询执行失败: ' . $stmt->error); } $appId = $stmt->insert_id; $stmt->close(); // 插入应用标签关联 foreach ($tags as $tagId) { $tagStmt = $conn->prepare('INSERT INTO app_tags (app_id, tag_id) VALUES (?, ?)'); if (!$tagStmt) { throw new Exception('标签关联查询准备失败: ' . $conn->error); } $tagStmt->bind_param('ii', $appId, $tagId); if (!$tagStmt->execute()) { throw new Exception('标签关联查询执行失败: ' . $tagStmt->error); } $tagStmt->close(); } // 插入应用图片 foreach ($imagePaths as $imagePath) { $imageStmt = $conn->prepare('INSERT INTO app_images (app_id, image_path) VALUES (?, ?)'); if (!$imageStmt) { throw new Exception('图片关联查询准备失败: ' . $conn->error); } $imageStmt->bind_param('is', $appId, $imageRelativePath); if (!$imageStmt->execute()) { throw new Exception('图片关联查询执行失败: ' . $imageStmt->error); } $imageStmt->close(); } // 插入应用版本信息 $versionStmt = $conn->prepare('INSERT INTO app_versions (app_id, version, changelog, file_path, created_at) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)'); if (!$versionStmt) { throw new Exception('版本信息查询准备失败: ' . $conn->error); } $versionStmt->bind_param('isss', $appId, $version, $changelog, $appRelativePath); if (!$versionStmt->execute()) { throw new Exception('版本信息查询执行失败: ' . $versionStmt->error); } $versionStmt->close(); // 提交事务 $conn->commit(); $success = '应用上传成功,请等待管理员审核'; } catch (Exception $e) { // 回滚事务 $conn->rollback(); log_error('应用上传事务失败: ' . $e->getMessage(), __FILE__, __LINE__); $error = '上传应用时发生错误,请稍后再试'; } } } } } ?>