file.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream'),
  12. 'bin' => 'application/macbinary',
  13. 'dms' => 'application/octet-stream',
  14. 'lha' => 'application/octet-stream',
  15. 'lzh' => 'application/octet-stream',
  16. 'exe' => array('application/octet-stream', 'application/x-msdownload'),
  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' => array('application/pdf', 'application/x-download'),
  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' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),
  31. 'ppt' => array('application/powerpoint', 'application/vnd.ms-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' => array('application/x-httpd-php', 'text/x-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' => array('application/x-tar', 'application/x-gzip-compressed'),
  50. 'xhtml' => 'application/xhtml+xml',
  51. 'xht' => 'application/xhtml+xml',
  52. 'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
  53. 'mid' => 'audio/midi',
  54. 'midi' => 'audio/midi',
  55. 'mpga' => 'audio/mpeg',
  56. 'mp2' => 'audio/mpeg',
  57. 'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'),
  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' => array('image/jpeg', 'image/pjpeg'),
  70. 'jpg' => array('image/jpeg', 'image/pjpeg'),
  71. 'jpe' => array('image/jpeg', 'image/pjpeg'),
  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' => array('text/plain', 'text/x-log'),
  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' => array('application/msword', 'application/octet-stream'),
  97. 'xl' => 'application/excel',
  98. 'eml' => 'message/rfc822',
  99. 'json' => array('application/json', 'text/json'),
  100. );
  101. /**
  102. * Get the contents of a file.
  103. *
  104. * @param string $path
  105. * @return string
  106. */
  107. public static function get($path)
  108. {
  109. return file_get_contents($path);
  110. }
  111. /**
  112. * Write to a file.
  113. *
  114. * @param string $path
  115. * @param string $data
  116. * @return int
  117. */
  118. public static function put($path, $data)
  119. {
  120. return file_put_contents($path, $data, LOCK_EX);
  121. }
  122. /**
  123. * Append to a file.
  124. *
  125. * @param string $path
  126. * @param string $data
  127. * @return int
  128. */
  129. public static function append($path, $data)
  130. {
  131. return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
  132. }
  133. /**
  134. * Extract the extension from a file path.
  135. *
  136. * @param string $path
  137. * @return string
  138. */
  139. public static function extension($path)
  140. {
  141. return pathinfo($path, PATHINFO_EXTENSION);
  142. }
  143. /**
  144. * Get a file MIME type by extension.
  145. *
  146. * @param string $extension
  147. * @param string $default
  148. * @return string
  149. */
  150. public static function mime($extension, $default = 'application/octet-stream')
  151. {
  152. if (array_key_exists($extension, static::$mimes))
  153. {
  154. return (is_array(static::$mimes[$extension])) ? static::$mimes[$extension][0] : static::$mimes[$extension];
  155. }
  156. return $default;
  157. }
  158. /**
  159. * Determine if a file is a given type.
  160. *
  161. * The Fileinfo PHP extension will be used to determine the MIME
  162. * type of the file. Any extension in the File::$mimes array may
  163. * be passed as a type.
  164. */
  165. public static function is($extension, $path)
  166. {
  167. if ( ! array_key_exists($extension, static::$mimes))
  168. {
  169. throw new \Exception("File extension [$extension] is unknown. Cannot determine file type.");
  170. }
  171. $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
  172. return (is_array(static::$mimes[$extension])) ? in_array($mime, static::$mimes[$extension]) : $mime === static::$mimes[$extension];
  173. }
  174. /**
  175. * Create a response that will force a file to be downloaded.
  176. *
  177. * @param string $path
  178. * @param string $name
  179. * @return Response
  180. */
  181. public static function download($path, $name = null)
  182. {
  183. if (is_null($name))
  184. {
  185. $name = basename($path);
  186. }
  187. $response = Response::make(static::get($path));
  188. $response->header('Content-Description', 'File Transfer');
  189. $response->header('Content-Type', static::mime(static::extension($path)));
  190. $response->header('Content-Disposition', 'attachment; filename="'.$name.'"');
  191. $response->header('Content-Transfer-Encoding', 'binary');
  192. $response->header('Expires', 0);
  193. $response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
  194. $response->header('Pragma', 'public');
  195. $response->header('Content-Length', filesize($path));
  196. return $response;
  197. }
  198. /**
  199. * Move an uploaded file to storage.
  200. *
  201. * @param string $key
  202. * @param string $path
  203. * @return bool
  204. */
  205. public static function upload($key, $path)
  206. {
  207. if ( ! array_key_exists($key, $_FILES))
  208. {
  209. return false;
  210. }
  211. return move_uploaded_file($_FILES[$key]['tmp_name'], $path);
  212. }
  213. }