'application/pdf', 'doc' => 'application/msword', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'xls' => 'application/vnd.ms-excel', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'ppt' => 'application/vnd.ms-powerpoint', 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'txt' => 'text/plain', 'csv' => 'text/csv', 'zip' => 'application/zip', 'rar' => 'application/x-rar-compressed', '7z' => 'application/x-7z-compressed', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'bmp' => 'image/bmp', 'svg' => 'image/svg+xml', 'mp3' => 'audio/mpeg', 'mp4' => 'video/mp4', 'avi' => 'video/x-msvideo', 'html' => 'text/html', 'htm' => 'text/html', 'xml' => 'text/xml', 'json' => 'application/json' ]; // Use browser-provided MIME type as fallback, or determine from extension $mimeType = isset($mimeTypes[$extension]) ? $mimeTypes[$extension] : (!empty($file['type']) ? $file['type'] : 'application/octet-stream'); // Generate unique filename $filename = uniqid() . '_' . time() . '.' . $extension; $filePath = DATA_DIR.'/uploads/' . $filename; // Move uploaded file if (move_uploaded_file($file['tmp_name'], $filePath)) { try { saveDocument($title, $description, $filename, $originalFilename, $filePath, $fileSize, $mimeType, $categoryId); ob_end_clean(); header('Location: documents.php?uploaded=1'); exit; } catch (Exception $e) { $error = "Failed to save document to database."; // Remove uploaded file if database save fails if (file_exists($filePath)) { unlink($filePath); } } } else { $error = "Failed to upload file."; } } } // Handle document update if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'update') { $id = intval($_POST['document_id']); $title = trim($_POST['title'] ?? ''); $description = trim($_POST['description'] ?? ''); $categoryId = isset($_POST['category_id']) && $_POST['category_id'] !== '' ? intval($_POST['category_id']) : null; if (empty($title)) { $error = "Title is required."; } else { try { updateDocument($id, $title, $description, $categoryId); ob_end_clean(); header('Location: documents.php?updated=1'); exit; } catch (Exception $e) { $error = "Failed to update document."; } } } // Handle document delete if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete') { $id = intval($_POST['document_id']); try { // Get document info to delete the file $document = getDocumentById($id); if ($document) { // Delete from database deleteDocument($id); // Delete file $file_path = DATA_DIR.'/uploads/'.$document['filename']; if (file_exists($file_path)) { unlink($file_path); } ob_end_clean(); header('Location: documents.php?deleted=1'); exit; } } catch (Exception $e) { $error = "Failed to delete document."; } } // Get all documents try { $documents = getAllDocuments(); } catch (Exception $e) { $error = "Failed to connect to database. Please check your configuration."; $documents = []; } // Get document for editing if edit mode $editDocument = null; if (isset($_GET['edit'])) { $editId = intval($_GET['edit']); $editDocument = getDocumentById($editId); } if (isset($_GET['uploaded'])) { $success = "Document uploaded successfully!"; } elseif (isset($_GET['updated'])) { $success = "Document updated successfully!"; } elseif (isset($_GET['deleted'])) { $success = "Document deleted successfully!"; } // Flush output buffer ob_end_flush(); ?>