This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-share.php
61 lines (52 loc) · 2.09 KB
/
process-share.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
<?php
require "./helpers/heading.php";
require_once "./helpers/db.php";
$target_dir = "images/";
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if user is logged in
loginGated();
// Check if file is uploaded
if (isset($_POST['img'])) {
// Check file size (the value is in bytes, so 2MB is 2097152 bytes)
if ($_FILES['img']['size'] > 2097152) {
echo "Sorry, your file is too large. It should be less than 2MB.";
http_response_code(400);
exit;
}
// // Check if it's an image file
// $check = getimagesize($_FILES['img']['tmp_name']);
// if($check === false) {
// echo "File is not an image.";
// exit;
// }
$dataURL = $_POST['img'];
$alt_text = htmlspecialchars(trim($_POST['alt_text']));
$caption = htmlspecialchars(trim($_POST['caption']));
list($type, $dataURL) = explode(';', $dataURL);
list(, $dataURL) = explode(',', $dataURL);
$data = base64_decode($dataURL);
$filename = uniqid() . '.png';
$target_file = $target_dir . $filename;
// Additional validations or processing can be added here
// Move uploaded file to a designated directory (optional)
if (file_put_contents($target_file, $data)) {
$author_id = $_SESSION['userId'];
$image_url = $target_file;
$stmt = $db->prepare("INSERT INTO posts (image_url, author_id, alt_text, caption) VALUES (:imageURL, :authorId, :altText, :caption)");
$stmt->execute(['imageURL' => $image_url, 'authorId' => $author_id, 'altText' => $alt_text, 'caption' => $caption]);
$stmt->closeCursor();
$postId = $db->lastInsertId();
echo "$postId";
} else {
echo "Sorry, there was an error uploading your file.";
http_response_code(400);
}
} else {
echo "No file was uploaded or there was an error in the upload.";
http_response_code(400);
}
}
?>