上传文件至 /
This commit is contained in:
37
delete-image.php
Normal file
37
delete-image.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
require_once 'config.php';
|
||||
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
header('Location: login.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
$image_id = $_GET['id'] ?? 0;
|
||||
|
||||
if ($image_id) {
|
||||
try {
|
||||
$stmt = $pdo->prepare("SELECT filename, user_id FROM images WHERE id = ?");
|
||||
$stmt->execute([$image_id]);
|
||||
$image = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($image && ($_SESSION['user_id'] == $image['user_id'] || $_SESSION['username'] === 'admin')) {
|
||||
$stmt = $pdo->prepare("DELETE FROM images WHERE id = ?");
|
||||
$stmt->execute([$image_id]);
|
||||
|
||||
$file_path = 'uploads/' . $image['filename'];
|
||||
if (file_exists($file_path)) {
|
||||
unlink($file_path);
|
||||
}
|
||||
|
||||
$_SESSION['success'] = t('image_deleted_success');
|
||||
} else {
|
||||
$_SESSION['error'] = t('no_permission_delete');
|
||||
}
|
||||
} catch(PDOException $e) {
|
||||
$_SESSION['error'] = t('delete_failed') . ': ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
header('Location: dashboard.php');
|
||||
exit;
|
||||
?>
|
||||
Reference in New Issue
Block a user