PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/kohana/file.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 179 lines | 75 code | 31 blank | 73 comment | 10 complexity | 03ae22242ab932d578f0a8b23b8f02a6 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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-2010 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 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('mimes');
  68. return isset($mimes[$extension]) ? $mimes[$extension][0] : FALSE;
  69. }
  70. /**
  71. * Split a file into pieces matching a specific size. Used when you need to
  72. * split large files into smaller pieces for easy transmission.
  73. *
  74. * $count = File::split($file);
  75. *
  76. * @param string file to be split
  77. * @param string directory to output to, defaults to the same directory as the file
  78. * @param integer size, in MB, for each piece to be
  79. * @return integer The number of pieces that were created
  80. */
  81. public static function split($filename, $piece_size = 10)
  82. {
  83. // Open the input file
  84. $file = fopen($filename, 'rb');
  85. // Change the piece size to bytes
  86. $piece_size = floor($piece_size * 1024 * 1024);
  87. // Write files in 8k blocks
  88. $block_size = 1024 * 8;
  89. // Total number of peices
  90. $peices = 0;
  91. while ( ! feof($file))
  92. {
  93. // Create another piece
  94. $peices += 1;
  95. // Create a new file piece
  96. $piece = str_pad($peices, 3, '0', STR_PAD_LEFT);
  97. $piece = fopen($filename.'.'.$piece, 'wb+');
  98. // Number of bytes read
  99. $read = 0;
  100. do
  101. {
  102. // Transfer the data in blocks
  103. fwrite($piece, fread($file, $block_size));
  104. // Another block has been read
  105. $read += $block_size;
  106. }
  107. while ($read < $piece_size);
  108. // Close the piece
  109. fclose($piece);
  110. }
  111. // Close the file
  112. fclose($file);
  113. return $peices;
  114. }
  115. /**
  116. * Join a split file into a whole file. Does the reverse of [File::split].
  117. *
  118. * $count = File::join($file);
  119. *
  120. * @param string split filename, without .000 extension
  121. * @param string output filename, if different then an the filename
  122. * @return integer The number of pieces that were joined.
  123. */
  124. public static function join($filename)
  125. {
  126. // Open the file
  127. $file = fopen($filename, 'wb+');
  128. // Read files in 8k blocks
  129. $block_size = 1024 * 8;
  130. // Total number of peices
  131. $pieces = 0;
  132. while (is_file($piece = $filename.'.'.str_pad($pieces + 1, 3, '0', STR_PAD_LEFT)))
  133. {
  134. // Read another piece
  135. $pieces += 1;
  136. // Open the piece for reading
  137. $piece = fopen($piece, 'rb');
  138. while ( ! feof($piece))
  139. {
  140. // Transfer the data in blocks
  141. fwrite($file, fread($piece, $block_size));
  142. }
  143. // Close the peice
  144. fclose($piece);
  145. }
  146. return $pieces;
  147. }
  148. } // End file