file.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php namespace System;
  2. class File {
  3. /**
  4. * Extensions and their matching MIME types.
  5. *
  6. * @var array
  7. */
  8. public static $mimes = array(
  9. 'hqx' => 'application/mac-binhex40',
  10. 'cpt' => 'application/mac-compactpro',
  11. 'csv' => 'text/x-comma-separated-values',
  12. 'bin' => 'application/macbinary',
  13. 'dms' => 'application/octet-stream',
  14. 'lha' => 'application/octet-stream',
  15. 'lzh' => 'application/octet-stream',
  16. 'exe' => 'application/octet-stream',
  17. 'class' => 'application/octet-stream',
  18. 'psd' => 'application/x-photoshop',
  19. 'so' => 'application/octet-stream',
  20. 'sea' => 'application/octet-stream',
  21. 'dll' => 'application/octet-stream',
  22. 'oda' => 'application/oda',
  23. 'pdf' => 'application/pdf',
  24. 'ai' => 'application/postscript',
  25. 'eps' => 'application/postscript',
  26. 'ps' => 'application/postscript',
  27. 'smi' => 'application/smil',
  28. 'smil' => 'application/smil',
  29. 'mif' => 'application/vnd.mif',
  30. 'xls' => 'application/excel',
  31. 'ppt' => 'application/powerpoint',
  32. 'wbxml' => 'application/wbxml',
  33. 'wmlc' => 'application/wmlc',
  34. 'dcr' => 'application/x-director',
  35. 'dir' => 'application/x-director',
  36. 'dxr' => 'application/x-director',
  37. 'dvi' => 'application/x-dvi',
  38. 'gtar' => 'application/x-gtar',
  39. 'gz' => 'application/x-gzip',
  40. 'php' => 'application/x-httpd-php',
  41. 'php4' => 'application/x-httpd-php',
  42. 'php3' => 'application/x-httpd-php',
  43. 'phtml' => 'application/x-httpd-php',
  44. 'phps' => 'application/x-httpd-php-source',
  45. 'js' => 'application/x-javascript',
  46. 'swf' => 'application/x-shockwave-flash',
  47. 'sit' => 'application/x-stuffit',
  48. 'tar' => 'application/x-tar',
  49. 'tgz' => 'application/x-tar',
  50. 'xhtml' => 'application/xhtml+xml',
  51. 'xht' => 'application/xhtml+xml',
  52. 'zip' => 'application/x-zip',
  53. 'mid' => 'audio/midi',
  54. 'midi' => 'audio/midi',
  55. 'mpga' => 'audio/mpeg',
  56. 'mp2' => 'audio/mpeg',
  57. 'mp3' => 'audio/mpeg',
  58. 'aif' => 'audio/x-aiff',
  59. 'aiff' => 'audio/x-aiff',
  60. 'aifc' => 'audio/x-aiff',
  61. 'ram' => 'audio/x-pn-realaudio',
  62. 'rm' => 'audio/x-pn-realaudio',
  63. 'rpm' => 'audio/x-pn-realaudio-plugin',
  64. 'ra' => 'audio/x-realaudio',
  65. 'rv' => 'video/vnd.rn-realvideo',
  66. 'wav' => 'audio/x-wav',
  67. 'bmp' => 'image/bmp',
  68. 'gif' => 'image/gif',
  69. 'jpeg' => 'image/jpeg',
  70. 'jpg' => 'image/jpeg',
  71. 'jpe' => 'image/jpeg',
  72. 'png' => 'image/png',
  73. 'tiff' => 'image/tiff',
  74. 'tif' => 'image/tiff',
  75. 'css' => 'text/css',
  76. 'html' => 'text/html',
  77. 'htm' => 'text/html',
  78. 'shtml' => 'text/html',
  79. 'txt' => 'text/plain',
  80. 'text' => 'text/plain',
  81. 'log' => 'text/plain',
  82. 'rtx' => 'text/richtext',
  83. 'rtf' => 'text/rtf',
  84. 'xml' => 'text/xml',
  85. 'xsl' => 'text/xml',
  86. 'mpeg' => 'video/mpeg',
  87. 'mpg' => 'video/mpeg',
  88. 'mpe' => 'video/mpeg',
  89. 'qt' => 'video/quicktime',
  90. 'mov' => 'video/quicktime',
  91. 'avi' => 'video/x-msvideo',
  92. 'movie' => 'video/x-sgi-movie',
  93. 'doc' => 'application/msword',
  94. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  95. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  96. 'word' => 'application/msword',
  97. 'xl' => 'application/excel',
  98. 'eml' => 'message/rfc822'
  99. );
  100. /**
  101. * Get the contents of a file.
  102. *
  103. * @param string $path
  104. * @return string
  105. */
  106. public static function get($path)
  107. {
  108. return file_get_contents($path);
  109. }
  110. /**
  111. * Write to a file.
  112. *
  113. * @param string $path
  114. * @param string $data
  115. * @return int
  116. */
  117. public static function put($path, $data)
  118. {
  119. return file_put_contents($path, $data, LOCK_EX);
  120. }
  121. /**
  122. * Append to a file.
  123. *
  124. * @param string $path
  125. * @param string $data
  126. * @return int
  127. */
  128. public static function append($path, $data)
  129. {
  130. return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
  131. }
  132. /**
  133. * Extract the extension from a file path.
  134. *
  135. * @param string $path
  136. * @return string
  137. */
  138. public static function extension($path)
  139. {
  140. return pathinfo($path, PATHINFO_EXTENSION);
  141. }
  142. /**
  143. * Get a file MIME type by extension.
  144. *
  145. * @param string $extension
  146. * @param string $default
  147. * @return string
  148. */
  149. public static function mime($extension, $default = 'application/octet-stream')
  150. {
  151. return (array_key_exists($extension, static::$mimes)) ? static::$mimes[$extension] : $default;
  152. }
  153. /**
  154. * Create a response that will force a file to be downloaded.
  155. *
  156. * @param string $path
  157. * @param string $name
  158. * @return Response
  159. */
  160. public static function download($path, $name = null)
  161. {
  162. if (is_null($name))
  163. {
  164. $name = basename($path);
  165. }
  166. $response = Response::make(static::get($path));
  167. $response->header('Content-Description', 'File Transfer');
  168. $response->header('Content-Type', static::mime(static::extension($path)));
  169. $response->header('Content-Disposition', 'attachment; filename="'.$name.'"');
  170. $response->header('Content-Transfer-Encoding', 'binary');
  171. $response->header('Expires', 0);
  172. $response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
  173. $response->header('Pragma', 'public');
  174. $response->header('Content-Length', filesize($path));
  175. return $response;
  176. }
  177. }