-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_files.php
64 lines (48 loc) · 1.95 KB
/
upload_files.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
session_start();
include 'db_connection.php';
if (!isset($_SESSION["UserID"])) {
header("Location: login.php");
exit();
}
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["taskID"])) {
$taskID = $_POST["taskID"];
$uploadDirectory = "uploads/";
if (!file_exists($uploadDirectory)) {
mkdir($uploadDirectory, 0777, true);
}
$uploadedFiles = [];
$stmt = $mysqli->prepare("SELECT ProjectID from task WHERE TaskID = ?");
$stmt->bind_param("i", $taskID);
$stmt->execute();
$stmt->bind_result($projectID);
$stmt->fetch();
$stmt->close();
if (!$projectID) {
echo json_encode(["success" => false, "message" => "Project not found for the given task"]);
exit();
}
foreach ($_FILES['files']['name'] as $key => $value) {
$fileName = basename($_FILES['files']['name'][$key]);
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
$uniqueFileName = $fileName . '_' . uniqid() . '.' . $fileExtension;
$targetFilePath = $uploadDirectory . $uniqueFileName;
//Move file to specified dir
if (move_uploaded_file($_FILES['files']['tmp_name'][$key], $targetFilePath)) {
$uploadedFiles[] = $uniqueFileName;
$fileID = $mysqli->insert_id;
$stmt = $mysqli->prepare("INSERT INTO file (FileName, FileType, UploadDate, UserID, TaskID, ProjectID) VALUES (?, ?, NOW(), ?, ?, ?)");
$stmt->bind_param("ssiii", $uniqueFileName, $_FILES['files']['type'][$key], $_SESSION["UserID"], $taskID, $projectID);
$stmt->execute();
$stmt->close();
} else {
echo json_encode(["success" => false, "message" => "Error uploading files"]);
exit();
}
}
$mysqli->close();
echo json_encode(["success" => true, "uploadedFiles" => $uploadedFiles]);
} else {
echo json_encode(["success" => false, "message" => "Invalid request"]);
}
?>