-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.php
More file actions
57 lines (45 loc) · 1.77 KB
/
post.php
File metadata and controls
57 lines (45 loc) · 1.77 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
53
54
55
56
57
<?php
// Enable error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include 'db_connection.php';
$userStatus = json_decode(file_get_contents('user_status.json'), true);
if (!$userStatus['logged_in']) {
echo "User not logged in.";
exit();
}
$user_id = $userStatus['user']['id'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$uploadedImages = [];
$targetDir = "uploads/";
if (!is_dir($targetDir)) {
mkdir($targetDir, 0777, true);
}
if (isset($_FILES['images']) && count($_FILES['images']['name']) > 0) {
for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
$targetFile = $targetDir . basename($_FILES['images']['name'][$i]);
$imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
exit;
}
if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $targetFile)) {
$uploadedImages[] = $targetFile;
} else {
echo "Sorry, there was an error uploading your file.";
exit();
}
}
}
$imagesJson = json_encode($uploadedImages);
$sql = "INSERT INTO BLOGS (date, user_id, images, upvotes, downvotes, comments) VALUES (NOW(), '$user_id', '$imagesJson', 0, 0, '[]')";
if (mysqli_query($conn, $sql)) {
header("Location: profile.html");
exit();
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
?>