getConnection();
$iduserlogin = (int) $iduserlogin;
function e($value): string
{
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
}
$uploadFeedback = null; // ['type' => 'success'|'error', 'text' => '...']
/* -------------------------------------------------------------------------
* Upload certificato (POST) con validazione robusta
* ---------------------------------------------------------------------- */
$allowedExt = ['pdf', 'jpg', 'jpeg', 'png'];
$allowedMime = ['application/pdf', 'image/jpeg', 'image/png'];
$maxBytes = 16 * 1024 * 1024; // 16 MB
$uploadDir = 'user/document/';
if (
$_SERVER['REQUEST_METHOD'] === 'POST'
&& isset($_FILES['fileToUpload'])
&& $_FILES['fileToUpload']['error'] === UPLOAD_ERR_OK
) {
$file = $_FILES['fileToUpload'];
$documentDescription = trim($_POST['documentDescription'] ?? '');
$expiryDate = trim($_POST['expiryDate'] ?? '');
if ($documentDescription === '' || $expiryDate === '') {
$uploadFeedback = ['type' => 'error', 'text' => 'Descrizione e data di scadenza sono obbligatorie.'];
} elseif ($file['size'] > $maxBytes) {
$uploadFeedback = ['type' => 'error', 'text' => 'Il file supera la dimensione massima di 16 MB.'];
} else {
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$finfo = new finfo(FILEINFO_MIME_TYPE);
$realMime = $finfo->file($file['tmp_name']);
if (!in_array($ext, $allowedExt, true) || !in_array($realMime, $allowedMime, true)) {
$uploadFeedback = ['type' => 'error', 'text' => 'Formato non consentito. Carica un PDF, JPG o PNG.'];
} else {
$safeName = bin2hex(random_bytes(16)) . '.' . $ext;
$destination = $uploadDir . $safeName;
if (!is_dir($uploadDir)) {
@mkdir($uploadDir, 0755, true);
}
if (move_uploaded_file($file['tmp_name'], $destination)) {
$sql = "INSERT INTO certificateuserprofile
(iduser, documentdescription, filenamedocument, expirydatedocument, uploaded_at)
VALUES (:iduser, :descr, :fname, :expiry, :uploaded)";
$stmt = $pdo->prepare($sql);
$ok = $stmt->execute([
':iduser' => $iduserlogin,
':descr' => $documentDescription,
':fname' => $safeName,
':expiry' => $expiryDate,
':uploaded' => date('Y-m-d'),
]);
$uploadFeedback = $ok
? ['type' => 'success', 'text' => 'Documento caricato correttamente.']
: ['type' => 'error', 'text' => 'Errore nel salvataggio del documento. Riprova.'];
} else {
$uploadFeedback = ['type' => 'error', 'text' => 'Caricamento del file non riuscito. Riprova.'];
}
}
}
} elseif (
$_SERVER['REQUEST_METHOD'] === 'POST'
&& isset($_FILES['fileToUpload'])
&& $_FILES['fileToUpload']['error'] !== UPLOAD_ERR_NO_FILE
) {
$uploadFeedback = ['type' => 'error', 'text' => 'Si è verificato un problema durante il caricamento. Riprova.'];
}
/* -------------------------------------------------------------------------
* Elenco documenti dell'utente
* ---------------------------------------------------------------------- */
$stmtDocs = $pdo->prepare("SELECT * FROM certificateuserprofile WHERE iduser = :iduser ORDER BY uploaded_at DESC");
$stmtDocs->execute([':iduser' => $iduserlogin]);
$documents = $stmtDocs->fetchAll();
?>
YogiBook - Carica Certificati
Certificato rimosso con successo.
I tuoi documenti
Ciao , qui trovi i certificati medici di liberatoria alla pratica Yoga che hai caricato.
| Descrizione |
Scadenza |
Documento |
Azione |
|
Nessun documento caricato. Usa il modulo qui sotto per aggiungere il primo.
|
|
|
Apri
|
|
Carica un nuovo documento
Formati accettati: PDF, JPG o PNG (max 16 MB).