import fogli di lavoro
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* attach_worksheet_file.php
|
||||
* -------------------------------------------------------------------------
|
||||
* Riceve worksheet_id + file .xlsx (campo "xlsx_file"), salva il file in
|
||||
* uploads/worksheets/ e lo collega al foglio (colonne source_xlsx_*).
|
||||
* Chiamato dal flusso di import DOPO il salvataggio della testata.
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
ini_set('display_errors', 0);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
while (ob_get_level()) {
|
||||
ob_end_clean();
|
||||
}
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
include('include/headscript.php');
|
||||
|
||||
function jexit($a)
|
||||
{
|
||||
echo json_encode($a, JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
jexit(['success' => false, 'message' => 'Metodo non valido']);
|
||||
}
|
||||
|
||||
$worksheet_id = isset($_POST['worksheet_id']) && is_numeric($_POST['worksheet_id'])
|
||||
? (int)$_POST['worksheet_id'] : 0;
|
||||
if ($worksheet_id <= 0) {
|
||||
jexit(['success' => false, 'message' => 'worksheet_id mancante o non valido']);
|
||||
}
|
||||
|
||||
if (!isset($_FILES['xlsx_file']) || $_FILES['xlsx_file']['error'] !== UPLOAD_ERR_OK) {
|
||||
jexit(['success' => false, 'message' => 'File non ricevuto']);
|
||||
}
|
||||
$file = $_FILES['xlsx_file'];
|
||||
|
||||
if ($file['size'] > 5 * 1024 * 1024) {
|
||||
jexit(['success' => false, 'message' => 'File troppo grande (max 5 MB)']);
|
||||
}
|
||||
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
|
||||
if ($ext !== 'xlsx') {
|
||||
jexit(['success' => false, 'message' => 'Sono ammessi solo file .xlsx']);
|
||||
}
|
||||
|
||||
$db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
|
||||
// il foglio deve esistere
|
||||
$chk = $pdo->prepare("SELECT id FROM work_sheets WHERE id = ?");
|
||||
$chk->execute([$worksheet_id]);
|
||||
if (!$chk->fetchColumn()) {
|
||||
jexit(['success' => false, 'message' => 'Foglio di lavoro inesistente']);
|
||||
}
|
||||
|
||||
// cartella di destinazione (accanto allo script)
|
||||
$dir = __DIR__ . '/uploads/worksheets';
|
||||
if (!is_dir($dir) && !mkdir($dir, 0775, true) && !is_dir($dir)) {
|
||||
jexit(['success' => false, 'message' => 'Impossibile creare la cartella uploads/worksheets']);
|
||||
}
|
||||
|
||||
$origName = $file['name'];
|
||||
$safeStored = 'ws_' . $worksheet_id . '_' . date('Ymd_His') . '.xlsx';
|
||||
$destAbs = $dir . '/' . $safeStored;
|
||||
$destRel = 'uploads/worksheets/' . $safeStored; // relativo alla cartella dello script
|
||||
|
||||
if (!move_uploaded_file($file['tmp_name'], $destAbs)) {
|
||||
jexit(['success' => false, 'message' => 'Salvataggio del file fallito']);
|
||||
}
|
||||
|
||||
$upd = $pdo->prepare("UPDATE work_sheets SET source_xlsx_name = ?, source_xlsx_path = ? WHERE id = ?");
|
||||
$upd->execute([$origName, $destRel, $worksheet_id]);
|
||||
|
||||
jexit(['success' => true, 'name' => $origName, 'path' => $destRel]);
|
||||
} catch (Exception $e) {
|
||||
jexit(['success' => false, 'message' => $e->getMessage()]);
|
||||
}
|
||||
Reference in New Issue
Block a user