-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_assignment_solution.php
More file actions
52 lines (41 loc) · 1.67 KB
/
Copy pathupload_assignment_solution.php
File metadata and controls
52 lines (41 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
session_start();
require 'config.php';
if (!isset($_SESSION['id']) || $_SESSION['role'] !== 'student') {
header('Location: index.php'); exit;
}
$user_id = $_SESSION['id'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$assignment_id = intval($_POST['assignment_id'] ?? 0);
if (!$assignment_id || !isset($_FILES['solution'])) {
die("Invalid request");
}
$uploaddir = __DIR__ . '/uploads/assignments/';
if (!is_dir($uploaddir)) mkdir($uploaddir, 0755, true);
$file = $_FILES['solution'];
if ($file['error'] !== UPLOAD_ERR_OK) die("Upload error");
// sanitize filename
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$safe = time() . '_' . preg_replace('/[^a-z0-9_\-\.]/i', '_', basename($file['name']));
$target = $uploaddir . $safe;
if (!move_uploaded_file($file['tmp_name'], $target)) {
die("Couldn't move uploaded file");
}
// store record
$stmt = $pdo->prepare("INSERT INTO assignment_uploads (assignment_id, user_id, filename, filepath) VALUES (?, ?, ?, ?)");
$stmt->execute([$assignment_id, $user_id, $file['name'], 'uploads/assignments/' . $safe]);
header("Location: student_dashboard.php?msg=uploaded");
exit;
}
// Simple form usage: include in student dashboard where assignments are listed
?>
<!DOCTYPE html>
<html>
<body>
<form action="upload_assignment_solution.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="assignment_id" value="<?= $a['id'] ?>">
<input type="file" name="solution" accept=".pdf,.doc,.docx,.zip" required>
<button type="submit">Upload solution</button>
</form>
</body>
</html>