gelöschte Bilder aus /uploads löschen
This commit is contained in:
@@ -77,6 +77,52 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
mkdir($uploadDir, 0755, true);
|
mkdir($uploadDir, 0755, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Verwaiste Bilder löschen:
|
||||||
|
try {
|
||||||
|
// 1. Alten Artikelzustand vor dem Update aus der Datenbank laden
|
||||||
|
$articleManager = ArticleManager::getInstance();
|
||||||
|
$oldArticle = $articleManager->getArticle($id);
|
||||||
|
$oldBlocks = json_decode($oldArticle->getContent(), true);
|
||||||
|
|
||||||
|
if (is_array($oldBlocks) && is_array($blocks)) {
|
||||||
|
$oldImages = [];
|
||||||
|
$newImages = [];
|
||||||
|
|
||||||
|
// Alle Bildpfade aus dem alten Zustand sammeln
|
||||||
|
foreach ($oldBlocks as $oldBlock) {
|
||||||
|
if (isset($oldBlock['type'], $oldBlock['value']) && $oldBlock['type'] === 'image' && !str_starts_with($oldBlock['value'], 'data:image/')) {
|
||||||
|
$oldImages[] = $oldBlock['value'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Alle Bildpfade aus dem neuen Zustand sammeln
|
||||||
|
foreach ($blocks as $newBlock) {
|
||||||
|
if (isset($newBlock['type'], $newBlock['value']) && $newBlock['type'] === 'image' && !str_starts_with($newBlock['value'], 'data:image/')) {
|
||||||
|
$newImages[] = $newBlock['value'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Differenz ermitteln: Welche Pfade waren alt da, sind neu aber weg?
|
||||||
|
$deletedImages = array_diff($oldImages, $newImages);
|
||||||
|
|
||||||
|
// Diese Dateien physisch vom Server löschen
|
||||||
|
foreach ($deletedImages as $imagePath) {
|
||||||
|
// Extra-Sicherheit: Nur Dateien im eigenen Uploads-Ordner löschen
|
||||||
|
$filename = basename($imagePath);
|
||||||
|
$fullDeletePath = $uploadDir . $filename;
|
||||||
|
|
||||||
|
if (file_exists($fullDeletePath) && is_file($fullDeletePath)) {
|
||||||
|
unlink($fullDeletePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$_SESSION["message"] = $e->getMessage();
|
||||||
|
header("location: ../../index.php?pfad=updateArticle&id=$id");
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- NEU hinzugefügte Base64-Bilder:
|
||||||
if (is_array($blocks)) {
|
if (is_array($blocks)) {
|
||||||
foreach ($blocks as &$block) {
|
foreach ($blocks as &$block) {
|
||||||
// Prüfen, ob der Block ein Bild ist und ein NEUES Bild (Base64-Format) enthält
|
// Prüfen, ob der Block ein Bild ist und ein NEUES Bild (Base64-Format) enthält
|
||||||
|
|||||||
Reference in New Issue
Block a user