<?php
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
$csv = $data['csv'] ?? '';
$fname = $data['filename'] ?? ('export_' . time() . '.csv');
if (!$csv) {
    echo json_encode(['success' => false, 'message' => 'No data!']); exit;
}
$tempdir = __DIR__ . '/TEMP/';
if (!is_dir($tempdir)) mkdir($tempdir, 0777, true);
$path = $tempdir . $fname;
file_put_contents($path, $csv);
echo json_encode(['success' => true, 'temp_path' => $path]);
