5 * 1024 * 1024) { // 5MB limit $_SESSION['message'] = 'File is too large. Maximum size is 5MB.'; $_SESSION['message_type'] = 'danger'; header('Location: view_application.php?id=' . $application_id); exit(); } if (move_uploaded_file($file['tmp_name'], $upload_path)) { try { $pdo = db(); $stmt = $pdo->prepare("INSERT INTO application_files (application_id, original_filename, stored_filename) VALUES (?, ?, ?)"); $stmt->execute([$application_id, $original_filename, $stored_filename]); $_SESSION['message'] = 'File uploaded successfully.'; $_SESSION['message_type'] = 'success'; } catch (PDOException $e) { // In a real app, log this error $_SESSION['message'] = 'Database error while saving file information.'; $_SESSION['message_type'] = 'danger'; // Optionally, delete the uploaded file if DB insert fails unlink($upload_path); } } else { $_SESSION['message'] = 'Failed to move uploaded file.'; $_SESSION['message_type'] = 'danger'; } } else { $_SESSION['message'] = 'File upload error. Please try again.'; $_SESSION['message_type'] = 'danger'; } header('Location: view_application.php?id=' . $application_id); exit(); } else { header('Location: index.php'); exit(); }