-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit_post.php
34 lines (27 loc) · 960 Bytes
/
submit_post.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
<?php
session_start();
if (!isset($_SESSION["username"])) {
header("Location: login.php");
exit();
}
$username = $_SESSION["username"];
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$content = $_POST["postContent"];
$image = null;
if (!empty($_FILES["postImage"]["name"])) {
$imageFilename = $_FILES["postImage"]["name"];
$imageUploadPath = "channels/$username/posts/" . $imageFilename;
move_uploaded_file($_FILES["postImage"]["tmp_name"], $imageUploadPath);
$image = $imageUploadPath;
}
$post = array(
"content" => $content,
"image" => $image
);
$postsData = json_decode(file_get_contents("channels/$username/posts.json"), true);
$postsData[] = $post;
file_put_contents("channels/$username/posts.json", json_encode($postsData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
header("Location: channel.php");
exit();
}
?>