File.class.php 5.2 KB

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