Files
fantacalcio-4h/upload.php
T

58 lines
1.6 KiB
PHP
Executable File

<?php
header('Content-Type: application/json');
require 'conversioni.php';
$response = [
'success' => false,
'message' => 'Si è verificato un errore sconosciuto.'
];
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$response['message'] = 'Metodo di richiesta non valido.';
echo json_encode($response);
exit;
}
if (!isset($_POST['type']) || !isset($_FILES['csv-file'])) {
$response['message'] = 'Dati mancanti nella richiesta.';
echo json_encode($response);
exit;
}
if ($_FILES['csv-file']['error'] !== UPLOAD_ERR_OK) {
$response['message'] = 'Errore durante il caricamento del file. Codice: ' . $_FILES['csv-file']['error'];
echo json_encode($response);
exit;
}
$type = $_POST['type'];
$csvContent = file_get_contents($_FILES['csv-file']['tmp_name']);
$jsonResult = false;
$destinationFile = '';
if ($type === 'giocatori') {
$jsonResult = importaGiocatori($csvContent);
$destinationFile = 'giocatori.json';
$response['message'] = 'Giocatori importati con successo!';
} elseif ($type === 'voti') {
$jsonResult = importaVoti($csvContent);
$destinationFile = 'voti.json';
$response['message'] = 'Voti importati con successo!';
} else {
$response['message'] = 'Tipo di importazione non valido.';
echo json_encode($response);
exit;
}
if ($jsonResult === false) {
$response['message'] = 'Errore durante la conversione del file CSV.';
} else {
if (file_put_contents($destinationFile, $jsonResult) !== false) {
$response['success'] = true;
} else {
$response['message'] = 'Errore durante il salvataggio del file JSON.';
}
}
echo json_encode($response);