|
1
|
|
|
<?php |
|
2
|
|
|
return function($type, $allowedFileTypes, $conn) { |
|
3
|
|
|
if(isset($_SESSION['user'])) { |
|
4
|
|
|
$fileType = strtolower(pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION)); |
|
5
|
|
|
$target_dir = __DIR__ . "/../dynamic/" . $type . "/"; |
|
6
|
|
|
$target_name = md5_file($_FILES["fileToUpload"]["tmp_name"]) . "." . $fileType; |
|
7
|
|
|
$target_file = $target_dir . $target_name; |
|
8
|
|
|
$uploadOk = true; |
|
9
|
|
|
$movedFile = 0; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
if (file_exists($target_file)) { |
|
13
|
|
|
$movedFile = true; |
|
14
|
|
|
} else { |
|
15
|
|
|
$movedFile = move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
if(!in_array($fileType, $allowedFileTypes)) { |
|
19
|
|
|
echo 'unsupported file type. must be one of ' . join(", ", $allowedFileTypes) . '<hr>'; |
|
20
|
|
|
$uploadOk = false; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
if ($uploadOk) { |
|
24
|
|
|
if ($movedFile) { |
|
25
|
|
|
$stmt = $conn->prepare("INSERT INTO files (type, title, extrainfo, author, filename) VALUES (?, ?, ?, ?, ?)"); |
|
26
|
|
|
$stmt->bind_param("sssss", $type, $title, $description, $_SESSION['user'], $filename); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
$filename = htmlspecialchars($target_name); |
|
|
|
|
|
|
29
|
|
|
$title = htmlspecialchars($_POST['title']); |
|
|
|
|
|
|
30
|
|
|
$description = htmlspecialchars($_POST['description']); |
|
31
|
|
|
$description = str_replace(PHP_EOL, "<br>", $description); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$stmt->execute(); |
|
34
|
|
|
$stmt->close(); |
|
35
|
|
|
} else { |
|
36
|
|
|
echo 'fatal error<hr>'; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} else { |
|
40
|
|
|
echo "You aren't logged in."; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
?> |
|
|
|
|
|