PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/system/classes/kohana/file.php

https://bitbucket.org/alvinpd/monsterninja
PHP | 186 lines | 79 code | 33 blank | 74 comment | 10 complexity | 8d8af0b7e66782fc30b1eb746fe937bd MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /**
  3. * File helper class.
  4. *
  5. * @package Kohana
  6. * @category Helpers
  7. * @author Kohana Team
  8. * @copyright (c) 2007-2009 Kohana Team
  9. * @license http://kohanaphp.com/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 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(FILEINFO_MIME))
  39. {
  40. return $info->file($filename);
  41. }
  42. return $mime;
  43. }
  44. if (ini_get('mime_magic.magicfile') AND function_exists('mime_content_type'))
  45. {
  46. // The mime_content_type function is only useful with a magic file
  47. return mime_content_type($filename);
  48. }
  49. if ( ! empty($extension))
  50. {
  51. return File::mime_by_ext($extension);
  52. }
  53. // Unable to find the mime-type
  54. return FALSE;
  55. }
  56. /**
  57. * Return the mime type of an extension.
  58. *
  59. * $mime = File::mime_by_ext('png'); // "image/png"
  60. *
  61. * @param string extension: php, pdf, txt, etc
  62. * @return string mime type on success
  63. * @return FALSE on failure
  64. */
  65. public static function mime_by_ext($extension)
  66. {
  67. // Load all of the mime types
  68. $mimes = Kohana::config('mimes');
  69. return isset($mimes[$extension]) ? $mimes[$extension][0] : FALSE;
  70. }
  71. /**
  72. * Split a file into pieces matching a specific size. Used when you need to
  73. * split large files into smaller pieces for easy transmission.
  74. *
  75. * $count = File::split($file);
  76. *
  77. * @param string file to be split
  78. * @param string directory to output to, defaults to the same directory as the file
  79. * @param integer size, in MB, for each piece to be
  80. * @return integer The number of pieces that were created
  81. */
  82. public static function split($filename, $piece_size = 10)
  83. {
  84. // Open the input file
  85. $file = fopen($filename, 'rb');
  86. // Change the piece size to bytes
  87. $piece_size = floor($piece_size * 1024 * 1024);
  88. // Write files in 8k blocks
  89. $block_size = 1024 * 8;
  90. // Total number of peices
  91. $peices = 0;
  92. while ( ! feof($file))
  93. {
  94. // Create another piece
  95. $peices += 1;
  96. // Create a new file piece
  97. $piece = str_pad($peices, 3, '0', STR_PAD_LEFT);
  98. $piece = fopen($filename.'.'.$piece, 'wb+');
  99. // Number of bytes read
  100. $read = 0;
  101. do
  102. {
  103. // Transfer the data in blocks
  104. fwrite($piece, fread($file, $block_size));
  105. // Another block has been read
  106. $read += $block_size;
  107. }
  108. while ($read < $piece_size);
  109. // Close the piece
  110. fclose($piece);
  111. }
  112. // Close the file
  113. fclose($file);
  114. return $peices;
  115. }
  116. /**
  117. * Join a split file into a whole file. Does the reverse of [File::split].
  118. *
  119. * $count = File::join($file);
  120. *
  121. * @param string split filename, without .000 extension
  122. * @param string output filename, if different then an the filename
  123. * @return integer The number of pieces that were joined.
  124. */
  125. public static function join($filename)
  126. {
  127. // Open the file
  128. $file = fopen($filename, 'wb+');
  129. // Read files in 8k blocks
  130. $block_size = 1024 * 8;
  131. // Total number of peices
  132. $pieces = 0;
  133. while (is_file($piece = $filename.'.'.str_pad($pieces + 1, 3, '0', STR_PAD_LEFT)))
  134. {
  135. // Read another piece
  136. $pieces += 1;
  137. // Open the piece for reading
  138. $piece = fopen($piece, 'rb');
  139. while ( ! feof($piece))
  140. {
  141. // Transfer the data in blocks
  142. fwrite($file, fread($piece, $block_size));
  143. }
  144. // Close the peice
  145. fclose($piece);
  146. }
  147. return $pieces;
  148. }
  149. final private function __construct()
  150. {
  151. // This is a static class
  152. }
  153. } // End file