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\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);

View File

@ -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);
}
}

View File

@ -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")) {