PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/kohana_core/system/helpers/file.php

https://gitlab.com/vince.omega/mcb-nov-build
PHP | 186 lines | 86 code | 33 blank | 67 comment | 15 complexity | 45ae24aee6be07a7c2d1944286a5d070 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * File helper class.
  4. *
  5. * $Id: file.php 3769 2008-12-15 00:48:56Z zombor $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class file_Core {
  13. /**
  14. * Attempt to get the mime type from a file. This method is horribly
  15. * unreliable, due to PHP being horribly unreliable when it comes to
  16. * determining the mime-type of a file.
  17. *
  18. * @param string filename
  19. * @return string mime-type, if found
  20. * @return boolean FALSE, if not found
  21. */
  22. public static function mime($filename)
  23. {
  24. // Make sure the file is readable
  25. if ( ! (is_file($filename) AND is_readable($filename)))
  26. return FALSE;
  27. // Get the extension from the filename
  28. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  29. if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension))
  30. {
  31. // Disable error reporting
  32. $ER = error_reporting(0);
  33. // Use getimagesize() to find the mime type on images
  34. $mime = getimagesize($filename);
  35. // Turn error reporting back on
  36. error_reporting($ER);
  37. // Return the mime type
  38. if (isset($mime['mime']))
  39. return $mime['mime'];
  40. }
  41. if (function_exists('finfo_open'))
  42. {
  43. // Use the fileinfo extension
  44. $finfo = finfo_open(FILEINFO_MIME);
  45. $mime = finfo_file($finfo, $filename);
  46. finfo_close($finfo);
  47. // Return the mime type
  48. return $mime;
  49. }
  50. if (ini_get('mime_magic.magicfile') AND function_exists('mime_content_type'))
  51. {
  52. // Return the mime type using mime_content_type
  53. return mime_content_type($filename);
  54. }
  55. if ( ! KOHANA_IS_WIN)
  56. {
  57. // Attempt to locate use the file command, checking the return value
  58. if ($command = trim(exec('which file', $output, $return)) AND $return === 0)
  59. {
  60. return trim(exec($command.' -bi '.escapeshellarg($filename)));
  61. }
  62. }
  63. if ( ! empty($extension) AND is_array($mime = Kohana::config('mimes.'.$extension)))
  64. {
  65. // Return the mime-type guess, based on the extension
  66. return $mime[0];
  67. }
  68. // Unable to find the mime-type
  69. return FALSE;
  70. }
  71. /**
  72. * Split a file into pieces matching a specific size.
  73. *
  74. * @param string file to be split
  75. * @param string directory to output to, defaults to the same directory as the file
  76. * @param integer size, in MB, for each chunk to be
  77. * @return integer The number of pieces that were created.
  78. */
  79. public static function split($filename, $output_dir = FALSE, $piece_size = 10)
  80. {
  81. // Find output dir
  82. $output_dir = ($output_dir == FALSE) ? pathinfo(str_replace('\\', '/', realpath($filename)), PATHINFO_DIRNAME) : str_replace('\\', '/', realpath($output_dir));
  83. $output_dir = rtrim($output_dir, '/').'/';
  84. // Open files for writing
  85. $input_file = fopen($filename, 'rb');
  86. // Change the piece size to bytes
  87. $piece_size = 1024 * 1024 * (int) $piece_size; // Size in bytes
  88. // Set up reading variables
  89. $read = 0; // Number of bytes read
  90. $piece = 1; // Current piece
  91. $chunk = 1024 * 8; // Chunk size to read
  92. // Split the file
  93. while ( ! feof($input_file))
  94. {
  95. // Open a new piece
  96. $piece_name = $filename.'.'.str_pad($piece, 3, '0', STR_PAD_LEFT);
  97. $piece_open = @fopen($piece_name, 'wb+') or die('Could not write piece '.$piece_name);
  98. // Fill the current piece
  99. while ($read < $piece_size AND $data = fread($input_file, $chunk))
  100. {
  101. fwrite($piece_open, $data) or die('Could not write to open piece '.$piece_name);
  102. $read += $chunk;
  103. }
  104. // Close the current piece
  105. fclose($piece_open);
  106. // Prepare to open a new piece
  107. $read = 0;
  108. $piece++;
  109. // Make sure that piece is valid
  110. ($piece < 999) or die('Maximum of 999 pieces exceeded, try a larger piece size');
  111. }
  112. // Close input file
  113. fclose($input_file);
  114. // Returns the number of pieces that were created
  115. return ($piece - 1);
  116. }
  117. /**
  118. * Join a split file into a whole 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, $output = FALSE)
  125. {
  126. if ($output == FALSE)
  127. $output = $filename;
  128. // Set up reading variables
  129. $piece = 1; // Current piece
  130. $chunk = 1024 * 8; // Chunk size to read
  131. // Open output file
  132. $output_file = @fopen($output, 'wb+') or die('Could not open output file '.$output);
  133. // Read each piece
  134. while ($piece_open = @fopen(($piece_name = $filename.'.'.str_pad($piece, 3, '0', STR_PAD_LEFT)), 'rb'))
  135. {
  136. // Write the piece into the output file
  137. while ( ! feof($piece_open))
  138. {
  139. fwrite($output_file, fread($piece_open, $chunk));
  140. }
  141. // Close the current piece
  142. fclose($piece_open);
  143. // Prepare for a new piece
  144. $piece++;
  145. // Make sure piece is valid
  146. ($piece < 999) or die('Maximum of 999 pieces exceeded');
  147. }
  148. // Close the output file
  149. fclose($output_file);
  150. // Return the number of pieces joined
  151. return ($piece - 1);
  152. }
  153. } // End file