file.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 copyFolder(string $_folder, string $_target)
  91. {
  92. if($dir = opendir($_folder)){
  93. mkdir($_target);
  94. while (($file = readdir($dir))) {
  95. if (($file != '.') && ($file != '..')) {
  96. if (is_dir($_folder . '/' . $file)) {
  97. self::copyFolder($_folder . '/' . $file, $_target . '/' . $file);
  98. } else {
  99. copy($_folder . '/' . $file, $_target . '/' . $file);
  100. }
  101. }
  102. }
  103. closedir($dir);
  104. }
  105. }
  106. public static function deleteFolder(string $_dir)
  107. {
  108. if(is_dir($_dir)){
  109. $command = "rm -r " . $_dir;
  110. try {
  111. system($command);
  112. return TRUE;
  113. } catch (Exception $ex) {
  114. return FALSE;
  115. }
  116. }
  117. }
  118. public static function zip(string $_dir, string $_name)
  119. {
  120. $zip = new ZipArchive();
  121. $zip_name = $_name . ".zip";
  122. $zip->open($zip_name, ZipArchive::CREATE);
  123. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($_dir), RecursiveIteratorIterator::LEAVES_ONLY);
  124. foreach ($files as $file) {
  125. if (!$file->isDir()) {
  126. $filePath = $file->getRealPath();
  127. $relativePath = substr($filePath, strlen($_dir));
  128. $zip->addFile($filePath, $relativePath);
  129. }
  130. }
  131. $zip->close();
  132. return $zip_name;
  133. }
  134. public static function unzip(string $_zip, string $_target){
  135. if(is_file($_zip)){
  136. $zipInfo = pathinfo($_zip);
  137. if($zipInfo["extension"] == "zip") {
  138. $command = "unzip -q " . $_zip . " -d " . $_target . $zipInfo["filename"];
  139. try {
  140. system($command);
  141. return $zipInfo["filename"];
  142. } catch (Exception $ex) {
  143. return FALSE;
  144. }
  145. }
  146. }
  147. return FALSE;
  148. }
  149. }