added mescole files drag drop
This commit is contained in:
@@ -16,10 +16,6 @@ if (!array_key_exists($categoria, $categorie)) {
|
||||
echo json_encode(['success' => false, 'message' => 'Categoria non valida']);
|
||||
exit;
|
||||
}
|
||||
if ($titolo === '') {
|
||||
echo json_encode(['success' => false, 'message' => 'Il titolo del file è obbligatorio']);
|
||||
exit;
|
||||
}
|
||||
if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
|
||||
echo json_encode(['success' => false, 'message' => 'File mancante o upload non riuscito']);
|
||||
exit;
|
||||
@@ -34,6 +30,16 @@ $tmpPath = $_FILES['file']['tmp_name'];
|
||||
$size = (int)$_FILES['file']['size'];
|
||||
$ext = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
|
||||
|
||||
// Titolo automatico dal nome file se non fornito
|
||||
if ($titolo === '') {
|
||||
$titolo = pathinfo($originalName, PATHINFO_FILENAME);
|
||||
$titolo = trim(preg_replace('/[_\-]+/', ' ', $titolo));
|
||||
if ($titolo === '') {
|
||||
$titolo = 'Documento del ' . date('d/m/Y');
|
||||
}
|
||||
}
|
||||
$titolo = mb_substr($titolo, 0, 255);
|
||||
|
||||
if (!in_array($ext, $allowedExt, true)) {
|
||||
echo json_encode(['success' => false, 'message' => 'Estensione file non consentita (' . $ext . ')']);
|
||||
exit;
|
||||
@@ -72,7 +78,25 @@ try {
|
||||
");
|
||||
$stmt->execute([$idmescola, $categoria, $titolo, $storedName, $originalName, $mimeType, $size]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $pdo->lastInsertId()]);
|
||||
$newId = (int)$pdo->lastInsertId();
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'id' => $newId,
|
||||
'row' => [
|
||||
'id' => $newId,
|
||||
'idmescola' => $idmescola,
|
||||
'categoria' => $categoria,
|
||||
'categoria_label' => $categorie[$categoria],
|
||||
'titolo' => $titolo,
|
||||
'filename' => $storedName,
|
||||
'original_filename' => $originalName,
|
||||
'mime_type' => $mimeType,
|
||||
'filesize' => $size,
|
||||
'uploaded_at' => date('Y-m-d H:i:s'),
|
||||
'url' => 'uploads/mescole/' . $idmescola . '/' . $storedName,
|
||||
],
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
// rollback file se il DB fallisce
|
||||
@unlink($destPath);
|
||||
|
||||
Reference in New Issue
Block a user