-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.php
45 lines (37 loc) · 1.29 KB
/
form.php
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
<?php
require 'vendor/autoload.php';
$searchQuery = isset($_POST['searchQuery']) ? $_POST['searchQuery'] : '';
$uploadDir = 'uploads/';
$uploadedFile = null;
if (isset($_FILES['syllabusFile'])) {
$uploadedFile = $uploadDir . basename($_FILES['syllabusFile']['name']);
if (move_uploaded_file($_FILES['syllabusFile']['tmp_name'], $uploadedFile)) {
try {
$mongoClient = new MongoDB\Client("mongodb://localhost:27017");
} catch (MongoDB\Driver\Exception\Exception $e) {
echo "Failed to connect to MongoDB: " . $e->getMessage();
exit;
}
$database = 'syllabusReader';
$collectionName = 'searchQueries';
$database = $mongoClient->$database;
$collection = $database->$collectionName;
$document = [
'searchQuery' => $searchQuery,
'timestamp' => new MongoDB\BSON\UTCDateTime(),
'file' => $uploadedFile
];
try {
$collection->insertOne($document);
header('Location: http://localhost:5000/upload');
exit;
} catch (MongoDB\Driver\Exception\Exception $e) {
echo "Error inserting document: " . $e->getMessage();
exit;
}
} else {
echo "Upload failed.\n";
exit;
}
}
?>