From 463f06ec02ecc02a70147b6494e23ec93ca74420 Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Sat, 17 Aug 2019 19:18:57 +0200 Subject: Media oragnisation - Delete folder - move or delete files --- app/class/modelmedia.php | 109 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 3 deletions(-) (limited to 'app/class/modelmedia.php') diff --git a/app/class/modelmedia.php b/app/class/modelmedia.php index ead7375..c2c1b47 100644 --- a/app/class/modelmedia.php +++ b/app/class/modelmedia.php @@ -154,10 +154,11 @@ class Modelmedia extends Model } - public function listdir($dir) + /** + * Generate an reccursive array where each folder is a array and containing a filecount in each folder + */ + public function listdir(string $dir) : array { - - $result = array(); $cdir = scandir($dir); @@ -175,6 +176,25 @@ class Modelmedia extends Model return $result; } + /** + * Analyse reccursive array of content to generate list of path + * + * @param array $dirlist Array generated by the listdir function + * @param string $parent used to create the strings + * @param array $pathlist used by reference, must be an empty array + * + * @return array list of path as string + */ + public function listpath(array $dirlist, string $parent = '', array &$pathlist = []) + { + foreach ($dirlist as $dir => $content) { + if(is_array($content)) { + $pathlist[] = $parent . $dir . DIRECTORY_SEPARATOR; + $this->listpath($content, $parent . $dir . DIRECTORY_SEPARATOR, $pathlist); + } + } + } + /** * Upload single file * @@ -238,4 +258,87 @@ class Modelmedia extends Model return false; } } + + /** + * Completely delete dir and it's content + * + * @param string $dir Directory to destroy + * + * @return bool depending on operation success + */ + public function deletedir(string $dir) : bool + { + if(substr($dir, -1) !== '/') { + $dir .= '/'; + } + if(is_dir($dir)) { + return $this->deltree($dir); + } else { + return false; + } + } + + /** + * Function do reccursively delete a directory + */ + public function deltree(string $dir) + { + $files = array_diff(scandir($dir), array('.','..')); + foreach ($files as $file) { + (is_dir("$dir/$file")) ? $this->deltree("$dir/$file") : unlink("$dir/$file"); + } + return rmdir($dir); + } + + /** + * Delete a file + */ + public function deletefile(string $filedir) + { + if(is_file($filedir)) { + return unlink($filedir); + } else { + return false; + } + } + + public function multifiledelete(array $filelist) + { + foreach ($filelist as $filedir ) { + if(is_string($filedir)) { + $this->deletefile($filedir); + } + } + } + + public function movefile(string $filedir, string $dir) + { + if(substr($dir, -1) !== '/') { + $dir .= '/'; + } + if(is_file($filedir)) { + $newdir = $dir . basename($filedir); + return rename($filedir, $newdir); + } else { + return false; + } + } + + public function multimovefile(array $filedirlist, string $dir) + { + $success = []; + foreach ($filedirlist as $filedir ) { + if(is_string($filedir)) { + $success[] = $this->movefile($filedir, $dir); + } + } + if(in_array(false, $success)) { + return false; + } else { + return true; + } + } + + + } -- cgit v1.2.3