File.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. class File{
  3. //Gère l'upload d'un fichier des vérifications au stockage dans le dossier file
  4. public static function upload($index,$path,$maxSize = 1048576,$extensions = array('jpg','png','jpeg','gif','bmp') ,$trimParentDir = true){
  5. if($trimParentDir) $path = str_replace('..','',$path);
  6. if(!isset($_FILES[$index])) throw new Exception("L'index fichier '".$index."' n'existe pas");
  7. if($_FILES[$index]['error'] != 0) throw new Exception("Erreur lors de l'envois du fichier : N° ".$_FILES[$index]['error']);
  8. $extension = getExt($_FILES[$index]['name']);
  9. if($_FILES[$index]['size'] > $maxSize) throw new Exception("Taille du fichier trop grande, taille maximum :".readable_size($maxSize).' ('.$maxSize.' octets)');
  10. if(!in_array($extension , $extensions)) throw new Exception("Extension '".$extension."' du fichier non permise, autorisé :".implode(', ',$extensions));
  11. $filePath = str_replace(array('/','\\','{{ext}}'),array(SLASH,SLASH,$extension),$path);
  12. $fileName = basename($filePath);
  13. $folderPath = dirname($filePath);
  14. $folderAbsolutePath = self::dir().$folderPath;
  15. $fileAbsolutePath = $folderAbsolutePath.SLASH.$fileName;
  16. if(!file_exists($folderAbsolutePath)) mkdir($folderAbsolutePath,0755,true);
  17. if(!move_uploaded_file($_FILES[$index]['tmp_name'], $fileAbsolutePath)) throw new Exception("Impossible de déplacer le fichier envoyé");
  18. return array(
  19. 'absolute' => $fileAbsolutePath,
  20. 'relative' => $filePath,
  21. 'name' => $fileName
  22. );
  23. }
  24. public static function move($file,$path,$trimParentDir = true){
  25. if($trimParentDir){
  26. $file = str_replace('..','',$file);
  27. $path = str_replace('..','',$path);
  28. }
  29. $extension = getExt($path);
  30. $filePath = str_replace(array('/','\\','{{ext}}'),array(SLASH,SLASH,$extension),$path);
  31. $fileName = basename($filePath);
  32. $folderPath = dirname($filePath);
  33. $folderAbsolutePath = self::dir().$folderPath;
  34. $fileAbsolutePath = $folderAbsolutePath.SLASH.$fileName;
  35. if(!file_exists($folderAbsolutePath)) mkdir($folderAbsolutePath,0755,true);
  36. if(!rename($file, $fileAbsolutePath)) throw new Exception("Impossible de déplacer le fichier envoyé");
  37. //New version
  38. //Testée, pour le moment pas de pb survenus
  39. return array(
  40. 'absolute' => (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($fileAbsolutePath)) : $fileAbsolutePath),
  41. 'relative' => (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($filePath)) : $filePath),
  42. 'name' => (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($fileName)) : $fileName)
  43. );
  44. //Old version
  45. // return array(
  46. // 'absolute' => $fileAbsolutePath,
  47. // 'relative' => $filePath,
  48. // 'name' => $fileName
  49. // );
  50. }
  51. public static function delete($namespace,$file,$trimParentDir = true){
  52. if($trimParentDir) $file = str_replace('..','',$file);
  53. $file = $namespace.SLASH.substr($file, strlen($namespace)+1);
  54. $file = File::dir().$file;
  55. $file = str_replace(array('\\','/'),array(SLASH,SLASH),$file);
  56. if(!file_exists($file)) throw new Exception("Le fichier '".$file."' n'existe pas");
  57. unlink($file);
  58. }
  59. public static function downloadFile($path,$name=null,$mime = null,$forceDownload = null,$trimParentDir = true){
  60. if($trimParentDir) $path = str_replace('..','',$path);
  61. if(!file_exists($path)) throw new Exception("Fichier inexistant :".$path);
  62. $stream = file_get_contents($path);
  63. if(!isset($mime)){
  64. $mime = mime_content_type($path);
  65. }
  66. if(!isset($name)){
  67. $name = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_encode(basename($path)) : basename($path);
  68. }
  69. ob_end_clean();
  70. header("Content-Type: ".$mime);
  71. header("Content-Length: " . strlen($stream));
  72. header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
  73. header('Cache-Control: no-store, no-cache, must-revalidate');
  74. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  75. header('Cache-Control: post-check=0, pre-check=0', FALSE);
  76. header('Pragma: no-cache');
  77. header("Content-Disposition: ".($forceDownload?"attachment":"inline")."; filename=\"".utf8_decode($name)."\"");
  78. echo $stream;
  79. }
  80. public static function downloadStream($stream, $name='Sans titre', $mime='application/octet-stream'){
  81. ob_end_clean();
  82. header("Content-Type: ".$mime);
  83. header("Content-Length: " . strlen($stream));
  84. header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
  85. header('Cache-Control: no-store, no-cache, must-revalidate');
  86. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  87. header('Cache-Control: post-check=0, pre-check=0', FALSE);
  88. header('Pragma: no-cache');
  89. header("Content-Disposition: attachment; filename=\"".$name."\"");
  90. echo $stream;
  91. }
  92. public static function dir(){
  93. return __ROOT__.FILE_PATH;
  94. }
  95. public static function temp(){
  96. $path = self::dir().SLASH.'tmp'.SLASH;
  97. if(!file_exists($path)) mkdir($path,0755,true);
  98. return $path;
  99. }
  100. public static function clear_temp(){
  101. $time = time();
  102. foreach(glob(self::temp().'*.*') as $file){
  103. if($time - filemtime($file) > 3600) unlink($file);
  104. }
  105. }
  106. public static function copy($source,$destination,$trimParentDir = true){
  107. if($trimParentDir) $source = str_replace('..','',$source);
  108. if($trimParentDir) $destination = str_replace('..','',$destination);
  109. if(is_file($source)) return copy($source,$destination);
  110. $dir = opendir($source);
  111. if(!file_exists($destination)) mkdir($destination,0755,true);
  112. while(false !== ( $file = readdir($dir)) ) {
  113. if (( $file != '.' ) && ( $file != '..' )) {
  114. if ( is_dir($source . SLASH . $file) ) {
  115. self::copy($source.SLASH.$file,$destination.SLASH.$file);
  116. }
  117. else {
  118. copy($source.SLASH.$file,$destination.SLASH.$file);
  119. }
  120. }
  121. }
  122. closedir($dir);
  123. }
  124. public static function convert_encoding($path){
  125. return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? utf8_encode($path) : $path;
  126. }
  127. public static function convert_decoding($path){
  128. return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? utf8_decode($path) : $path;
  129. }
  130. }
  131. ?>