diff options
author | vincent-peugnet <v.peugnet@free.fr> | 2019-08-17 19:18:57 +0200 |
---|---|---|
committer | vincent-peugnet <v.peugnet@free.fr> | 2019-08-17 19:18:57 +0200 |
commit | 463f06ec02ecc02a70147b6494e23ec93ca74420 (patch) | |
tree | 3bc470b8ff253dc343ca9b60b0cdf4a19c784236 /app/class/modelmedia.php | |
parent | 0d863868d69d9e2c9ae3f6f0b22b4d3a1be73ee2 (diff) | |
download | wcms-463f06ec02ecc02a70147b6494e23ec93ca74420.tar.gz wcms-463f06ec02ecc02a70147b6494e23ec93ca74420.zip |
Media oragnisation
- Delete folder
- move or delete files
Diffstat (limited to 'app/class/modelmedia.php')
-rw-r--r-- | app/class/modelmedia.php | 109 |
1 files changed, 106 insertions, 3 deletions
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); @@ -176,6 +177,25 @@ class Modelmedia extends Model } /** + * 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 * * @param string $index The file id @@ -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; + } + } + + + } |