2
0

file.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. class file
  3. {
  4. public static function record(array $_temp, string $_folderFiles = DIR_DATAS_FILES)
  5. {
  6. $md5 = md5_file($_temp["tmp_name"]);
  7. if (copy($_temp["tmp_name"], $_folderFiles . $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($_folderFiles . $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 = NULL, string $_folderFiles = DIR_DATAS_FILES)
  24. {
  25. if (isset($_id) and $_id != NULL and file_exists($_folderFiles . $_id)) {
  26. if (unlink($_folderFiles . $_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 lors de la désindexation 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, string $_folderFiles = DIR_DATAS_FILES)
  45. {
  46. if (file_exists($_folderFiles . $_id)) {
  47. return $_folderFiles . $_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 cleanAllFiles(string $_path)
  78. {
  79. $return = TRUE;
  80. $dir = new DirectoryIterator($_path);
  81. $blackList = array(
  82. ".",
  83. "..",
  84. "index.html",
  85. "index.php"
  86. );
  87. foreach ($dir as $fileinfo) {
  88. if (!in_array($fileinfo->getFilename(), $blackList)) {
  89. $return = (unlink($_path . $fileinfo->getFilename())) ? TRUE : FALSE;
  90. }
  91. }
  92. return $return;
  93. }
  94. public static function cleanFilesByTime(string $_path, int $_limitTime = 24 * 3600) // 24*3600 pour une journée
  95. {
  96. if ($handle = opendir($_path)) {
  97. while (false !== ($file = readdir($handle))) {
  98. $filelastmodified = filemtime($_path . $file);
  99. if ((time() - $filelastmodified) > $_limitTime) {
  100. unlink($_path . $file);
  101. }
  102. }
  103. closedir($handle);
  104. }
  105. }
  106. public static function copyFolder(string $_folder, string $_target)
  107. {
  108. if ($dir = opendir($_folder)) {
  109. mkdir($_target);
  110. while (($file = readdir($dir))) {
  111. if (($file != '.') && ($file != '..')) {
  112. if (is_dir($_folder . '/' . $file)) {
  113. self::copyFolder($_folder . '/' . $file, $_target . '/' . $file);
  114. } else {
  115. copy($_folder . '/' . $file, $_target . '/' . $file);
  116. }
  117. }
  118. }
  119. closedir($dir);
  120. }
  121. }
  122. public static function deleteFolder(string $_dir)
  123. {
  124. if (is_dir($_dir)) {
  125. $command = "rm -r " . $_dir;
  126. try {
  127. system($command);
  128. return TRUE;
  129. } catch (Exception $ex) {
  130. return FALSE;
  131. }
  132. }
  133. }
  134. public static function zip(string $_dir, string $_name)
  135. {
  136. $zip = new ZipArchive();
  137. $zip_name = $_name . ".zip";
  138. $zip->open($zip_name, ZipArchive::CREATE);
  139. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($_dir), RecursiveIteratorIterator::LEAVES_ONLY);
  140. foreach ($files as $file) {
  141. if (!$file->isDir()) {
  142. $filePath = $file->getRealPath();
  143. $relativePath = substr($filePath, strlen($_dir));
  144. $zip->addFile($filePath, $relativePath);
  145. }
  146. }
  147. $zip->close();
  148. return $zip_name;
  149. }
  150. public static function unzip(string $_zip, string $_target)
  151. {
  152. if (is_file($_zip)) {
  153. $zipInfo = pathinfo($_zip);
  154. if ($zipInfo["extension"] == "zip") {
  155. $command = "unzip -q " . $_zip . " -d " . $_target . $zipInfo["filename"];
  156. try {
  157. system($command);
  158. return $zipInfo["filename"];
  159. } catch (Exception $ex) {
  160. return FALSE;
  161. }
  162. }
  163. }
  164. return FALSE;
  165. }
  166. public static function sizeFolder(string $_rep)
  167. {
  168. $Racine = opendir($_rep);
  169. $Taille = 0;
  170. while ($Dossier = readdir($Racine)) {
  171. if ($Dossier != '..' and $Dossier != '.') {
  172. //Ajoute la taille du sous dossier
  173. if (is_dir($_rep . '/' . $Dossier)) $Taille += self::sizeFolder($_rep . '/' .
  174. $Dossier);
  175. //Ajoute la taille du fichier
  176. else $Taille += filesize($_rep . '/' . $Dossier);
  177. }
  178. }
  179. closedir($Racine);
  180. return $Taille;
  181. }
  182. }