-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_image_message.php
More file actions
62 lines (51 loc) · 1.87 KB
/
upload_image_message.php
File metadata and controls
62 lines (51 loc) · 1.87 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
58
59
60
61
62
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lost_found_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$response = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$sender_id = $_POST['sender_id'];
$created_at = date('Y-m-d H:i:s');
if (isset($_FILES['image']) && $_FILES['image']['error'] == UPLOAD_ERR_OK) {
$fileTmpPath = $_FILES['image']['tmp_name'];
$fileName = $_FILES['image']['name'];
$uploadPath = "uploads/img_messages/" . $fileName;
// Move the uploaded file to the designated path
if (move_uploaded_file($fileTmpPath, $uploadPath)) {
// Fetch all admin IDs from the admins table
$admin_ids = array();
$admin_query = "SELECT id FROM admins";
$result = $conn->query($admin_query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$admin_ids[] = $row['id'];
}
}
// Insert a single entry to avoid duplicates
$stmt = $conn->prepare("INSERT INTO admin_message (sender_id, receiver_id, media_url, created_at) VALUES (?, ?, ?, ?)");
$success = true;
$stmt->bind_param("iiss", $sender_id, $admin_ids[0], $uploadPath, $created_at);
if (!$stmt->execute()) {
$success = false;
}
$response['success'] = $success;
} else {
$response['success'] = false;
$response['error'] = 'File upload failed';
}
} else {
$response['success'] = false;
$response['error'] = 'No file uploaded or file error';
}
} else {
$response['success'] = false;
}
echo json_encode($response);
$stmt->close();
$conn->close();
?>