-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_image.php
More file actions
46 lines (37 loc) · 1006 Bytes
/
delete_image.php
File metadata and controls
46 lines (37 loc) · 1006 Bytes
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
<?php
require_once "includes/auth.php";
require_once "includes/db.php";
$image_id = (int)($_GET['id'] ?? 0);
$car_id = (int)($_GET['car_id'] ?? 0);
if ($image_id <= 0 || $car_id <= 0) {
header("Location: index.php");
exit;
}
$stmt = $pdo->prepare("
SELECT ci.file_name, c.user_id
FROM car_images ci
JOIN cars c ON ci.car_id = c.id
WHERE ci.id = ? AND ci.car_id = ?
");
$stmt->execute([$image_id, $car_id]);
$image = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$image) {
header("Location: index.php");
exit;
}
// 🔐 μόνο ο ιδιοκτήτης
if ($image['user_id'] != $_SESSION['user_id']) {
http_response_code(403);
exit;
}
// 🗑️ filesystem
$filePath = __DIR__ . "/uploads/" . $image['file_name'];
if (is_file($filePath)) {
unlink($filePath);
}
// 🗑️ database
$stmt = $pdo->prepare("DELETE FROM car_images WHERE id = ?");
$stmt->execute([$image_id]);
// 🔁 πίσω στο όχημα
header("Location: car_view.php?id=" . $car_id);
exit;