PageRenderTime 85ms CodeModel.GetById 32ms RepoModel.GetById 2ms app.codeStats 0ms

/system/helpers/file.php

https://github.com/Toushi/flow
PHP | 177 lines | 79 code | 32 blank | 66 comment | 13 complexity | 0e34a081d9a858d3d3f5bb5d72a4ab06 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * File helper class.
  4. *
  5. * $Id: file.php 3237 2008-07-30 12:10:20Z Geert $
  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 ( ! empty($extension) AND is_array($mime = Kohana::config('mimes.'.$extension)))
  56. {
  57. // Return the mime-type guess, based on the extension
  58. return $mime[0];
  59. }
  60. // Unable to find the mime-type
  61. return FALSE;
  62. }
  63. /**
  64. * Split a file into pieces matching a specific size.
  65. *
  66. * @param string file to be split
  67. * @param string directory to output to, defaults to the same directory as the file
  68. * @param integer size, in MB, for each chunk to be
  69. * @return integer The number of pieces that were created.
  70. */
  71. public static function split($filename, $output_dir = FALSE, $piece_size = 10)
  72. {
  73. // Find output dir
  74. $output_dir = ($output_dir == FALSE) ? pathinfo(str_replace('\\', '/', realpath($filename)), PATHINFO_DIRNAME) : str_replace('\\', '/', realpath($output_dir));
  75. $output_dir = rtrim($output_dir, '/').'/';
  76. // Open files for writing
  77. $input_file = fopen($filename, 'rb');
  78. // Change the piece size to bytes
  79. $piece_size = 1024 * 1024 * (int) $piece_size; // Size in bytes
  80. // Set up reading variables
  81. $read = 0; // Number of bytes read
  82. $piece = 1; // Current piece
  83. $chunk = 1024 * 8; // Chunk size to read
  84. // Split the file
  85. while ( ! feof($input_file))
  86. {
  87. // Open a new piece
  88. $piece_name = $filename.'.'.str_pad($piece, 3, '0', STR_PAD_LEFT);
  89. $piece_open = @fopen($piece_name, 'wb+') or die('Could not write piece '.$piece_name);
  90. // Fill the current piece
  91. while ($read < $piece_size AND $data = fread($input_file, $chunk))
  92. {
  93. fwrite($piece_open, $data) or die('Could not write to open piece '.$piece_name);
  94. $read += $chunk;
  95. }
  96. // Close the current piece
  97. fclose($piece_open);
  98. // Prepare to open a new piece
  99. $read = 0;
  100. $piece++;
  101. // Make sure that piece is valid
  102. ($piece < 999) or die('Maximum of 999 pieces exceeded, try a larger piece size');
  103. }
  104. // Close input file
  105. fclose($input_file);
  106. // Returns the number of pieces that were created
  107. return ($piece - 1);
  108. }
  109. /**
  110. * Join a split file into a whole file.
  111. *
  112. * @param string split filename, without .000 extension
  113. * @param string output filename, if different then an the filename
  114. * @return integer The number of pieces that were joined.
  115. */
  116. public static function join($filename, $output = FALSE)
  117. {
  118. if ($output == FALSE)
  119. $output = $filename;
  120. // Set up reading variables
  121. $piece = 1; // Current piece
  122. $chunk = 1024 * 8; // Chunk size to read
  123. // Open output file
  124. $output_file = @fopen($output, 'wb+') or die('Could not open output file '.$output);
  125. // Read each piece
  126. while ($piece_open = @fopen(($piece_name = $filename.'.'.str_pad($piece, 3, '0', STR_PAD_LEFT)), 'rb'))
  127. {
  128. // Write the piece into the output file
  129. while ( ! feof($piece_open))
  130. {
  131. fwrite($output_file, fread($piece_open, $chunk));
  132. }
  133. // Close the current piece
  134. fclose($piece_open);
  135. // Prepare for a new piece
  136. $piece++;
  137. // Make sure piece is valid
  138. ($piece < 999) or die('Maximum of 999 pieces exceeded');
  139. }
  140. // Close the output file
  141. fclose($output_file);
  142. // Return the number of pieces joined
  143. return ($piece - 1);
  144. }
  145. } // End file