| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- class file
- {
- public static function record(array $_temp)
- {
- $md5 = md5_file($_temp["tmp_name"]);
- if(copy($_temp["tmp_name"], DIR_DATAS_FILES . $md5)){
- db::query("INSERT INTO ". DB_T_FILES ." (id, name, size, id_user) VALUES (:id, :name, :size, :id_user)");
- db::bind(':id', $md5);
- db::bind(':name', $_temp['name']);
- db::bind(':size', $_temp['size']);
- db::bind(':id_user', session::getId());
- try {
- db::execute();
- return $md5;
- } catch (Exception $ex) {
- unlink(DIR_DATAS_FILES . $md5);
- alert::recError("Erreur #record sur l'import du fichier " . $_temp['name']);
- return FALSE;
- }
- }
- }
- public static function delete(string $_id)
- {
- if (file_exists(DIR_DATAS_FILES . $_id)) {
- if(unlink(DIR_DATAS_FILES.$_id)){
- db::query("DELETE FROM ". DB_T_FILES ." WHERE id = :id");
- db::bind(':id', $_id);
- try {
- db::execute();
- return TRUE;
- } catch (Exception $ex) {
- alert::recError("Erreur sur la suppression du fichier");
- return FALSE;
- }
- } else {
- alert::recError("Erreur sur la suppression du fichier");
- return FALSE;
- }
- } else {
- return FALSE;
- }
- }
- public static function download(string $_id)
- {
- if (file_exists(DIR_DATAS_FILES . $_id)) {
- return DIR_DATAS_FILES . $_id;
- } else {
- return FALSE;
- }
- }
- public static function cleanFilesByOrder(string $_path, int $_nbFiles = 5)
- {
- $return = TRUE;
- $cpt = 0;
- $files = array();
- $dir = new DirectoryIterator($_path);
- $blackList = array(
- ".",
- "..",
- "index.html",
- "index.php"
- );
-
- foreach ($dir as $fileinfo) {
- $files[$fileinfo->getMTime()] = $fileinfo->getFilename();
- }
-
- krsort($files);
-
- foreach($files as $file) {
- if (!in_array($file, $blackList)) {
- if($cpt++ >= $_nbFiles){
- $return = (unlink($_path . $file)) ? TRUE : FALSE;
- }
- }
- }
- return $return;
- }
- public static function cleanFilesByTime(string $_path, int $_limitTime = 24*3600) // 24*3600 pour une journée
- {
- if ($handle = opendir($_path)) {
- while (false !== ($file = readdir($handle))) {
- $filelastmodified = filemtime($_path . $file);
- if((time() - $filelastmodified) > $_limitTime)
- {
- unlink($_path . $file);
- }
- }
- closedir($handle);
- }
- }
- public static function compress(string $_file){
- $command = "gzip " . $_file;
- try {
- system($command);
- return $_file.".gz";
- } catch (Exception $ex) {
- return FALSE;
- }
- }
- public static function decompress(string $_zip, bool $_delete = FALSE){
- $command = "gunzip " . $_zip;
- try {
- system($command);
- ($_delete == TRUE) ? unlink($_zip) : "";
- return explode(".gz", $_zip)[0];
- } catch (Exception $ex) {
- return FALSE;
- }
- }
- public static function copyFolder(string $_folder, string $_target)
- {
- if($dir = opendir($_folder)){
- mkdir($_target);
- while (($file = readdir($dir))) {
- if (($file != '.') && ($file != '..')) {
- if (is_dir($_folder . '/' . $file)) {
- self::copyFolder($_folder . '/' . $file, $_target . '/' . $file);
- } else {
- copy($_folder . '/' . $file, $_target . '/' . $file);
- }
- }
- }
- closedir($dir);
- }
- }
- }
|