file.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. class file
  3. {
  4. public static function record(array $_temp)
  5. {
  6. $md5 = md5_file($_temp["tmp_name"]);
  7. if(copy($_temp["tmp_name"], DIR_DATAS_FILES . $md5)){
  8. db::query("INSERT INTO ". DB_T_FILES ." (id, name, size, id_user) VALUES (:id, :name, :size, :id_user)");
  9. db::bind(':id', $md5);
  10. db::bind(':name', $_temp['name']);
  11. db::bind(':size', $_temp['size']);
  12. db::bind(':id_user', session::getId());
  13. try {
  14. db::execute();
  15. return $md5;
  16. } catch (Exception $ex) {
  17. unlink(DIR_DATAS_FILES . $md5);
  18. alert::recError("Erreur #record sur l'import du fichier " . $_temp['name']);
  19. return FALSE;
  20. }
  21. }
  22. }
  23. public static function delete(string $_id)
  24. {
  25. if (file_exists(DIR_DATAS_FILES . $_id)) {
  26. if(unlink(DIR_DATAS_FILES.$_id)){
  27. db::query("DELETE FROM ". DB_T_FILES ." WHERE id = :id");
  28. db::bind(':id', $_id);
  29. try {
  30. db::execute();
  31. return TRUE;
  32. } catch (Exception $ex) {
  33. alert::recError("Erreur sur la suppression du fichier");
  34. return FALSE;
  35. }
  36. } else {
  37. alert::recError("Erreur sur la suppression du fichier");
  38. return FALSE;
  39. }
  40. } else {
  41. return FALSE;
  42. }
  43. }
  44. public static function download(string $_id)
  45. {
  46. if (file_exists(DIR_DATAS_FILES . $_id)) {
  47. return DIR_DATAS_FILES . $_id;
  48. } else {
  49. return FALSE;
  50. }
  51. }
  52. public static function cleanFilesByOrder(string $_path, int $_nbFiles = 5)
  53. {
  54. $return = TRUE;
  55. $cpt = 0;
  56. $files = array();
  57. $dir = new DirectoryIterator($_path);
  58. $blackList = array(
  59. ".",
  60. "..",
  61. "index.html",
  62. "index.php"
  63. );
  64. foreach ($dir as $fileinfo) {
  65. $files[$fileinfo->getMTime()] = $fileinfo->getFilename();
  66. }
  67. krsort($files);
  68. foreach($files as $file) {
  69. if (!in_array($file, $blackList)) {
  70. if($cpt++ >= $_nbFiles){
  71. $return = (unlink($_path . $file)) ? TRUE : FALSE;
  72. }
  73. }
  74. }
  75. return $return;
  76. }
  77. public static function cleanFilesByTime(string $_path, int $_limitTime = 24*3600) // 24*3600 pour une journée
  78. {
  79. if ($handle = opendir($_path)) {
  80. while (false !== ($file = readdir($handle))) {
  81. $filelastmodified = filemtime($_path . $file);
  82. if((time() - $filelastmodified) > $_limitTime)
  83. {
  84. unlink($_path . $file);
  85. }
  86. }
  87. closedir($handle);
  88. }
  89. }
  90. public static function compress(string $_file){
  91. $command = "gzip " . $_file;
  92. try {
  93. system($command);
  94. return $_file.".gz";
  95. } catch (Exception $ex) {
  96. return FALSE;
  97. }
  98. }
  99. public static function decompress(string $_zip, bool $_delete = FALSE){
  100. $command = "gunzip " . $_zip;
  101. try {
  102. system($command);
  103. ($_delete == TRUE) ? unlink($_zip) : "";
  104. return explode(".gz", $_zip)[0];
  105. } catch (Exception $ex) {
  106. return FALSE;
  107. }
  108. }
  109. public static function copyFolder(string $_folder, string $_target)
  110. {
  111. if($dir = opendir($_folder)){
  112. mkdir($_target);
  113. while (($file = readdir($dir))) {
  114. if (($file != '.') && ($file != '..')) {
  115. if (is_dir($_folder . '/' . $file)) {
  116. self::copyFolder($_folder . '/' . $file, $_target . '/' . $file);
  117. } else {
  118. copy($_folder . '/' . $file, $_target . '/' . $file);
  119. }
  120. }
  121. }
  122. closedir($dir);
  123. }
  124. }
  125. }