File.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. //récupere le type mime du fichier
  25. public static function mime($path){
  26. $infos = new finfo();
  27. return $infos->file($path, FILEINFO_MIME_TYPE);
  28. }
  29. public static function move($file,$path,$trimParentDir = true){
  30. if($trimParentDir){
  31. $file = str_replace('..','',$file);
  32. $path = str_replace('..','',$path);
  33. }
  34. $extension = getExt($path);
  35. $filePath = str_replace(array('/','\\','{{ext}}'),array(SLASH,SLASH,$extension),$path);
  36. $fileName = basename($filePath);
  37. $folderPath = dirname($filePath);
  38. $folderAbsolutePath = self::dir().$folderPath;
  39. $fileAbsolutePath = $folderAbsolutePath.SLASH.$fileName;
  40. if(!file_exists($folderAbsolutePath)) mkdir($folderAbsolutePath,0755,true);
  41. if(!rename($file, $fileAbsolutePath)) throw new Exception("Impossible de déplacer le fichier envoyé");
  42. //New version
  43. //Testée, pour le moment pas de pb survenus
  44. return array(
  45. 'absolute' => (get_OS() === 'WIN' ? iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($fileAbsolutePath)) : $fileAbsolutePath),
  46. 'relative' => (get_OS() === 'WIN' ? iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($filePath)) : $filePath),
  47. 'name' => (get_OS() === 'WIN' ? iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($fileName)) : $fileName)
  48. );
  49. //Old version
  50. // return array(
  51. // 'absolute' => $fileAbsolutePath,
  52. // 'relative' => $filePath,
  53. // 'name' => $fileName
  54. // );
  55. }
  56. public static function delete($namespace,$file,$trimParentDir = true){
  57. if($trimParentDir) $file = str_replace('..','',$file);
  58. $file = $namespace.SLASH.substr($file, strlen($namespace)+1);
  59. $file = File::dir().$file;
  60. $file = str_replace(array('\\','/'),array(SLASH,SLASH),$file);
  61. if(!file_exists($file)) throw new Exception("Le fichier '".$file."' n'existe pas");
  62. unlink($file);
  63. }
  64. public static function downloadFile($path,$name=null,$mime = null,$forceDownload = null,$trimParentDir = true){
  65. if($trimParentDir) $path = str_replace('..','',$path);
  66. if(!file_exists($path)) throw new Exception("Fichier inexistant :".$path);
  67. $stream = file_get_contents($path);
  68. $mime = !isset($mime) ? self::mime($path) : $mime;
  69. if(!isset($name)){
  70. $name = (get_OS() === 'WIN') ? utf8_encode(basename($path)) : basename($path);
  71. }
  72. if (ob_get_contents()) ob_end_clean();
  73. header("Content-Type: ".$mime);
  74. header("Content-Length: " . strlen($stream));
  75. header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
  76. header('Cache-Control: no-store, no-cache, must-revalidate');
  77. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  78. header('Cache-Control: post-check=0, pre-check=0', FALSE);
  79. header('Pragma: no-cache');
  80. header("Content-Disposition: ".($forceDownload?"attachment":"inline")."; filename=\"".utf8_decode($name)."\"");
  81. echo $stream;
  82. }
  83. public static function downloadStream($stream, $name='Sans titre', $mime='application/octet-stream'){
  84. if (ob_get_contents()) ob_end_clean();
  85. header("Content-Type: ".$mime);
  86. header("Content-Length: " . strlen($stream));
  87. header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
  88. header('Cache-Control: no-store, no-cache, must-revalidate');
  89. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  90. header('Cache-Control: post-check=0, pre-check=0', FALSE);
  91. header('Pragma: no-cache');
  92. header("Content-Disposition: attachment; filename=\"".$name."\"");
  93. echo $stream;
  94. }
  95. public static function dir(){
  96. return __ROOT__.FILE_PATH;
  97. }
  98. public static function temp(){
  99. $path = self::dir().SLASH.'tmp'.SLASH;
  100. if(!file_exists($path)) mkdir($path,0755,true);
  101. return $path;
  102. }
  103. public static function clear_temp(){
  104. $time = time();
  105. foreach(glob(self::temp().'*.*') as $file){
  106. if($time - filemtime($file) > 3600) unlink($file);
  107. }
  108. }
  109. public static function copy($source,$destination,$trimParentDir = true){
  110. if($trimParentDir) $source = str_replace('..','',$source);
  111. if($trimParentDir) $destination = str_replace('..','',$destination);
  112. if(is_file($source)) return copy($source,$destination);
  113. $dir = opendir($source);
  114. if(!file_exists($destination)) mkdir($destination,0755,true);
  115. while(false !== ( $file = readdir($dir)) ) {
  116. if (( $file != '.' ) && ( $file != '..' )) {
  117. if ( is_dir($source . SLASH . $file) ) {
  118. self::copy($source.SLASH.$file,$destination.SLASH.$file);
  119. }
  120. else {
  121. copy($source.SLASH.$file,$destination.SLASH.$file);
  122. }
  123. }
  124. }
  125. closedir($dir);
  126. }
  127. public static function convert_encoding($path){
  128. return get_OS() === 'WIN' ? utf8_encode($path) : $path;
  129. }
  130. public static function convert_decoding($path){
  131. return get_OS() === 'WIN' ? utf8_decode($path) : $path;
  132. }
  133. }
  134. ?>