From 5ca5899368375113a69a83cd609d6c7c81d46ce7 Mon Sep 17 00:00:00 2001 From: Matthias Grief Date: Sun, 8 Dec 2024 16:43:51 +0100 Subject: [PATCH] =?UTF-8?q?L=C3=B6schen=20von=20downloadbaren=20Dateien=20?= =?UTF-8?q?eingebaut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webseite/classes/TopicData.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/webseite/classes/TopicData.php b/webseite/classes/TopicData.php index 3adea50..0396591 100644 --- a/webseite/classes/TopicData.php +++ b/webseite/classes/TopicData.php @@ -187,10 +187,20 @@ class TopicData */ private function addDownload(string $name, string $tmp_name): bool { - if(!move_uploaded_file($tmp_name, Config::getTopicDirectory($this->getSubjectId() , $this->getId()) . "downloads/" . $name)) { + $downloadDirectory = Config::getTopicDirectory($this->getSubjectId() , $this->getId()) . "downloads/"; + + if(!is_dir($downloadDirectory)) { + if(!mkdir($downloadDirectory)) { + return false; + } + } + + if(!move_uploaded_file($tmp_name, $downloadDirectory . $name)) { return false; } + $this->files[] = $name; + return true; } @@ -222,6 +232,21 @@ class TopicData return true; } + public function deleteDownload(string $name): bool + { + if(!isset($this->files[$name])) { + return false; + } + + if(!unlink(Config::getTopicDirectory($this->getSubjectId() , $this->getId()) . "downloads/$name")) { + return false; + } + + $this->files = array_diff($this->files, [$name]); + + return true; + } + /** * Löscht das Thema inklusive aller zugehörigen Dateien * @return bool true, wenn erfolgreich gelöscht, sonst false