typicalname0 /
4grounds
| 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); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
| 27 | |||
| 28 | $filename = htmlspecialchars($target_name); |
||
|
0 ignored issues
–
show
|
|||
| 29 | $title = htmlspecialchars($_POST['title']); |
||
|
0 ignored issues
–
show
|
|||
| 30 | $description = htmlspecialchars($_POST['description']); |
||
| 31 | $description = str_replace(PHP_EOL, "<br>", $description); |
||
|
0 ignored issues
–
show
|
|||
| 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 | ?> |
||
|
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. Loading history...
|