fonction d'upload fully working now !

This commit is contained in:
Florian Bouillon 2019-03-07 10:52:56 +01:00
parent a2f1200654
commit 194ca52738
3 changed files with 35 additions and 4 deletions

View File

@ -5,6 +5,7 @@ namespace App\Controller;
use App\Controller; use App\Controller;
use App\DB\Post; use App\DB\Post;
use App\DB\Tag; use App\DB\Tag;
use App\Functions;
class PostController extends Controller { class PostController extends Controller {
@ -80,14 +81,23 @@ class PostController extends Controller {
//move images //move images
$post = Post::list(true, 1)[0]; $post = Post::list(true, 1)[0];
$oldfolder = DIR."/../uploads/posts/new/"; $oldfolder = ROOT."/uploads/posts/new/";
$files = scandir($oldfolder); $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) { foreach($files as $fname) {
if($fname != '.' && $fname != '..') { if($fname != '.' && $fname != '..') {
var_dump($fname);
rename($oldfolder.$fname, $newfolder.$fname); rename($oldfolder.$fname, $newfolder.$fname);
} }
} }
var_dump($newfolder);
$post->setContent(str_replace("/uploads/posts/new/", "/uploads/posts/" . $post->getId() . "/",$post->getContent())); $post->setContent(str_replace("/uploads/posts/new/", "/uploads/posts/" . $post->getId() . "/",$post->getContent()));
Post::update($post); Post::update($post);
@ -112,21 +122,23 @@ class PostController extends Controller {
*/ */
public function delete() { public function delete() {
Post::remove(Post::get($_GET["post"])); Post::remove(Post::get($_GET["post"]));
Functions::deleteDir(ROOT."/uploads/posts/" . $_GET["post"] . "/");
header("Location: /"); header("Location: /");
} }
/** /**
* @route /^\/post\/[0-9]+\/upload\/$/ * @route /^\/post\/([0-9]+\/)*upload\/$/
*/ */
public function upload() { public function upload() {
if($_GET["post"] && $_FILES["file"]) { if($_GET["post"] && $_FILES["file"]) {
$post = $_GET['post']; $post = $_GET['post'];
if($post == "new") $post = "temp"; if($post == "upload") $post = "new";
$uploadFolder = DIR."/../uploads/posts/".$post."/"; $uploadFolder = DIR."/../uploads/posts/".$post."/";
var_dump($post);
if(!file_exists($uploadFolder)) { if(!file_exists($uploadFolder)) {
mkdir($uploadFolder, 0666, true); mkdir($uploadFolder, 0666, true);

View File

@ -73,4 +73,22 @@ class Functions {
$parent->appendChild($importedNode); $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);
}
} }

View File

@ -26,6 +26,7 @@ if(isset($_SESSION["author"])) { //wip
// var_dump($_SESSION["author"]); // var_dump($_SESSION["author"]);
define("DIR", str_replace("/php", "", __DIR__)); define("DIR", str_replace("/php", "", __DIR__));
define("ROOT", str_replace("/assets/php", "", __DIR__));
//renvoie vers le fichier css si il est demandé //renvoie vers le fichier css si il est demandé
if(Functions::endsWith($_GET["page"], ".css")) { if(Functions::endsWith($_GET["page"], ".css")) {