From 194ca527380bc3c5590f3c05d1fbb78cdd14635d Mon Sep 17 00:00:00 2001 From: Avior Date: Thu, 7 Mar 2019 10:52:56 +0100 Subject: [PATCH] fonction d'upload fully working now ! --- assets/php/Controller/PostController.php | 20 ++++++++++++++++---- assets/php/Functions.php | 18 ++++++++++++++++++ assets/php/handler.php | 1 + 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/assets/php/Controller/PostController.php b/assets/php/Controller/PostController.php index 4558550..6cb824e 100644 --- a/assets/php/Controller/PostController.php +++ b/assets/php/Controller/PostController.php @@ -5,6 +5,7 @@ namespace App\Controller; use App\Controller; use App\DB\Post; use App\DB\Tag; +use App\Functions; class PostController extends Controller { @@ -80,14 +81,23 @@ class PostController extends Controller { //move images $post = Post::list(true, 1)[0]; - $oldfolder = DIR."/../uploads/posts/new/"; + $oldfolder = ROOT."/uploads/posts/new/"; $files = scandir($oldfolder); - $newfolder = DIR."/../uploads/posts/" . $post->getId() . "/";; + var_dump($files); + // die; + $newfolder = ROOT."/uploads/posts/" . $post->getId() . "/"; + + if(!file_exists($newfolder)) { + mkdir($newfolder, 0666, true); + } + foreach($files as $fname) { if($fname != '.' && $fname != '..') { + var_dump($fname); rename($oldfolder.$fname, $newfolder.$fname); } } + var_dump($newfolder); $post->setContent(str_replace("/uploads/posts/new/", "/uploads/posts/" . $post->getId() . "/",$post->getContent())); Post::update($post); @@ -112,21 +122,23 @@ class PostController extends Controller { */ public function delete() { Post::remove(Post::get($_GET["post"])); + Functions::deleteDir(ROOT."/uploads/posts/" . $_GET["post"] . "/"); header("Location: /"); } /** - * @route /^\/post\/[0-9]+\/upload\/$/ + * @route /^\/post\/([0-9]+\/)*upload\/$/ */ public function upload() { if($_GET["post"] && $_FILES["file"]) { $post = $_GET['post']; - if($post == "new") $post = "temp"; + if($post == "upload") $post = "new"; $uploadFolder = DIR."/../uploads/posts/".$post."/"; + var_dump($post); if(!file_exists($uploadFolder)) { mkdir($uploadFolder, 0666, true); diff --git a/assets/php/Functions.php b/assets/php/Functions.php index 028bdc2..f0c0b7c 100644 --- a/assets/php/Functions.php +++ b/assets/php/Functions.php @@ -73,4 +73,22 @@ class Functions { $parent->appendChild($importedNode); } } + + public static function deleteDir($dirPath) { + if (! is_dir($dirPath)) { + throw new InvalidArgumentException("$dirPath must be a directory"); + } + if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') { + $dirPath .= '/'; + } + $files = glob($dirPath . '*', GLOB_MARK); + foreach ($files as $file) { + if (is_dir($file)) { + self::deleteDir($file); + } else { + unlink($file); + } + } + rmdir($dirPath); + } } diff --git a/assets/php/handler.php b/assets/php/handler.php index 6f8f9a6..77da715 100644 --- a/assets/php/handler.php +++ b/assets/php/handler.php @@ -26,6 +26,7 @@ if(isset($_SESSION["author"])) { //wip // var_dump($_SESSION["author"]); define("DIR", str_replace("/php", "", __DIR__)); +define("ROOT", str_replace("/assets/php", "", __DIR__)); //renvoie vers le fichier css si il est demandé if(Functions::endsWith($_GET["page"], ".css")) {