PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/kohana/file.php

https://bitbucket.org/girakon/blog-tutorial
PHP | 241 lines | 110 code | 38 blank | 93 comment | 14 complexity | ddc180acf1ab33e825c73dea2d9605df MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * File helper class.
  4. *
  5. * @package Kohana
  6. * @category Helpers
  7. * @author Kohana Team
  8. * @copyright (c) 2007-2012 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. */
  11. class Kohana_File {
  12. /**
  13. * Attempt to get the mime type from a file. This method is horribly
  14. * unreliable, due to PHP being horribly unreliable when it comes to
  15. * determining the mime type of a file.
  16. *
  17. * $mime = File::mime($file);
  18. *
  19. * @param string $filename file name or path
  20. * @return string mime type on success
  21. * @return FALSE on failure
  22. */
  23. public static function mime($filename)
  24. {
  25. // Get the complete path to the file
  26. $filename = realpath($filename);
  27. // Get the extension from the filename
  28. $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  29. if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension))
  30. {
  31. // Use getimagesize() to find the mime type on images
  32. $file = getimagesize($filename);
  33. if (isset($file['mime']))
  34. return $file['mime'];
  35. }
  36. if (class_exists('finfo', FALSE))
  37. {
  38. if ($info = new finfo(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME))
  39. {
  40. return $info->file($filename);
  41. }
  42. }
  43. if (ini_get('mime_magic.magicfile') AND function_exists('mime_content_type'))
  44. {
  45. // The mime_content_type function is only useful with a magic file
  46. return mime_content_type($filename);
  47. }
  48. if ( ! empty($extension))
  49. {
  50. return File::mime_by_ext($extension);
  51. }
  52. // Unable to find the mime-type
  53. return FALSE;
  54. }
  55. /**
  56. * Return the mime type of an extension.
  57. *
  58. * $mime = File::mime_by_ext('png'); // "image/png"
  59. *
  60. * @param string $extension php, pdf, txt, etc
  61. * @return string mime type on success
  62. * @return FALSE on failure
  63. */
  64. public static function mime_by_ext($extension)
  65. {
  66. // Load all of the mime types
  67. $mimes = Kohana::$config->load('mimes');
  68. return isset($mimes[$extension]) ? $mimes[$extension][0] : FALSE;
  69. }
  70. /**
  71. * Lookup MIME types for a file
  72. *
  73. * @see Kohana_File::mime_by_ext()
  74. * @param string $extension Extension to lookup
  75. * @return array Array of MIMEs associated with the specified extension
  76. */
  77. public static function mimes_by_ext($extension)
  78. {
  79. // Load all of the mime types
  80. $mimes = Kohana::$config->load('mimes');
  81. return isset($mimes[$extension]) ? ( (array) $mimes[$extension]) : array();
  82. }
  83. /**
  84. * Lookup file extensions by MIME type
  85. *
  86. * @param string $type File MIME type
  87. * @return array File extensions matching MIME type
  88. */
  89. public static function exts_by_mime($type)
  90. {
  91. static $types = array();
  92. // Fill the static array
  93. if (empty($types))
  94. {
  95. foreach (Kohana::$config->load('mimes') as $ext => $mimes)
  96. {
  97. foreach ($mimes as $mime)
  98. {
  99. if ($mime == 'application/octet-stream')
  100. {
  101. // octet-stream is a generic binary
  102. continue;
  103. }
  104. if ( ! isset($types[$mime]))
  105. {
  106. $types[$mime] = array( (string) $ext);
  107. }
  108. elseif ( ! in_array($ext, $types[$mime]))
  109. {
  110. $types[$mime][] = (string) $ext;
  111. }
  112. }
  113. }
  114. }
  115. return isset($types[$type]) ? $types[$type] : FALSE;
  116. }
  117. /**
  118. * Lookup a single file extension by MIME type.
  119. *
  120. * @param string $type MIME type to lookup
  121. * @return mixed First file extension matching or false
  122. */
  123. public static function ext_by_mime($type)
  124. {
  125. return current(File::exts_by_mime($type));
  126. }
  127. /**
  128. * Split a file into pieces matching a specific size. Used when you need to
  129. * split large files into smaller pieces for easy transmission.
  130. *
  131. * $count = File::split($file);
  132. *
  133. * @param string $filename file to be split
  134. * @param integer $piece_size size, in MB, for each piece to be
  135. * @return integer The number of pieces that were created
  136. */
  137. public static function split($filename, $piece_size = 10)
  138. {
  139. // Open the input file
  140. $file = fopen($filename, 'rb');
  141. // Change the piece size to bytes
  142. $piece_size = floor($piece_size * 1024 * 1024);
  143. // Write files in 8k blocks
  144. $block_size = 1024 * 8;
  145. // Total number of peices
  146. $peices = 0;
  147. while ( ! feof($file))
  148. {
  149. // Create another piece
  150. $peices += 1;
  151. // Create a new file piece
  152. $piece = str_pad($peices, 3, '0', STR_PAD_LEFT);
  153. $piece = fopen($filename.'.'.$piece, 'wb+');
  154. // Number of bytes read
  155. $read = 0;
  156. do
  157. {
  158. // Transfer the data in blocks
  159. fwrite($piece, fread($file, $block_size));
  160. // Another block has been read
  161. $read += $block_size;
  162. }
  163. while ($read < $piece_size);
  164. // Close the piece
  165. fclose($piece);
  166. }
  167. // Close the file
  168. fclose($file);
  169. return $peices;
  170. }
  171. /**
  172. * Join a split file into a whole file. Does the reverse of [File::split].
  173. *
  174. * $count = File::join($file);
  175. *
  176. * @param string $filename split filename, without .000 extension
  177. * @return integer The number of pieces that were joined.
  178. */
  179. public static function join($filename)
  180. {
  181. // Open the file
  182. $file = fopen($filename, 'wb+');
  183. // Read files in 8k blocks
  184. $block_size = 1024 * 8;
  185. // Total number of peices
  186. $pieces = 0;
  187. while (is_file($piece = $filename.'.'.str_pad($pieces + 1, 3, '0', STR_PAD_LEFT)))
  188. {
  189. // Read another piece
  190. $pieces += 1;
  191. // Open the piece for reading
  192. $piece = fopen($piece, 'rb');
  193. while ( ! feof($piece))
  194. {
  195. // Transfer the data in blocks
  196. fwrite($file, fread($piece, $block_size));
  197. }
  198. // Close the peice
  199. fclose($piece);
  200. }
  201. return $pieces;
  202. }
  203. } // End file