-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_image.php
More file actions
37 lines (28 loc) · 1.18 KB
/
upload_image.php
File metadata and controls
37 lines (28 loc) · 1.18 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
<?php
require 'db_connection.php';
session_start();
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$headerId = $_POST['header'] ?? null;
$caption = trim($_POST['caption']) ?? '';
$uploadDir = "uploads/";
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0777, true);
}
if (isset($_FILES["file"]) && $_FILES["file"]["error"] == 0) {
$fileName = basename($_FILES["file"]["name"]);
$targetPath = $uploadDir . $fileName;
if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetPath)) {
$stmt = $db->prepare("INSERT INTO file_uploads (header_id, caption, path) VALUES (:header_id, :caption, :path)");
$stmt->bindValue(':header_id', $headerId, SQLITE3_INTEGER);
$stmt->bindValue(':caption', $caption, SQLITE3_TEXT);
$stmt->bindValue(':path', $targetPath, SQLITE3_TEXT);
$stmt->execute();
echo json_encode(["status" => "success", "path" => $targetPath]);
} else {
echo json_encode(["status" => "error", "message" => "File upload failed"]);
}
} else {
echo json_encode(["status" => "error", "message" => "No file uploaded"]);
}
}
?>