5) { header('HTTP/1.0 403 Forbidden'); die('접근 권한이 없습니다.'); } // 녹음 파일 ID 확인 $consultation_id = isset($_GET['id']) ? intval($_GET['id']) : 0; $manager_id = $user_id; if ($consultation_id <= 0) { header('HTTP/1.0 400 Bad Request'); die('잘못된 요청입니다.'); } try { $pdo = db_connect(); $sql = "SELECT audio_file_path, created_at FROM manager_consultations WHERE id = ? AND manager_id = ?"; $stmt = $pdo->prepare($sql); $stmt->execute([$consultation_id, $manager_id]); $consultation = $stmt->fetch(PDO::FETCH_ASSOC); if (!$consultation || empty($consultation['audio_file_path'])) { header('HTTP/1.0 404 Not Found'); die('오디오 파일을 찾을 수 없습니다.'); } // GCS URI인 경우 처리 불가 (직접 다운로드 불가) if (strpos($consultation['audio_file_path'], 'gs://') === 0) { header('HTTP/1.0 400 Bad Request'); die('GCS에 저장된 파일은 직접 다운로드할 수 없습니다.'); } // 파일 경로 구성 $file_path = $_SERVER['DOCUMENT_ROOT'] . $consultation['audio_file_path']; // 경로 정규화 $file_path = str_replace('\\', '/', $file_path); $file_path = preg_replace('#/+#', '/', $file_path); // 파일 존재 확인 if (!file_exists($file_path)) { header('HTTP/1.0 404 Not Found'); die('오디오 파일이 서버에 존재하지 않습니다.'); } // 파일 확장자 확인 $file_extension = pathinfo($file_path, PATHINFO_EXTENSION) ?: 'webm'; $mime_types = [ 'webm' => 'audio/webm', 'wav' => 'audio/wav', 'mp3' => 'audio/mpeg', 'ogg' => 'audio/ogg', 'm4a' => 'audio/mp4' ]; $content_type = isset($mime_types[$file_extension]) ? $mime_types[$file_extension] : 'audio/webm'; // 다운로드 파일명 생성 $date = date('Ymd_His', strtotime($consultation['created_at'])); $download_filename = '상담녹음_' . $date . '.' . $file_extension; // 출력 버퍼 비우기 ob_clean(); $file_size = filesize($file_path); if ($file_size === false || $file_size == 0) { header('HTTP/1.0 500 Internal Server Error'); die('파일을 읽을 수 없습니다.'); } // 헤더 설정 header('Content-Type: ' . $content_type); header('Content-Disposition: attachment; filename="' . $download_filename . '"'); header('Content-Length: ' . $file_size); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Expires: 0'); // 파일 출력 $handle = @fopen($file_path, 'rb'); if ($handle === false) { header('HTTP/1.0 500 Internal Server Error'); die('파일을 열 수 없습니다.'); } while (!feof($handle)) { $chunk = fread($handle, 8192); if ($chunk === false) break; echo $chunk; flush(); } fclose($handle); exit; } catch (Exception $e) { header('HTTP/1.0 500 Internal Server Error'); die('파일 다운로드 중 오류가 발생했습니다.'); }