1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: file.php 3917 2009-01-21 03:06:22Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
15 * Attempt to get the mime type from a file. This method is horribly
16 * unreliable, due to PHP being horribly unreliable when it comes to
17 * determining the mime-type of a file.
19 * @param string filename
20 * @return string mime-type, if found
21 * @return boolean FALSE, if not found
23 public static function mime($filename)
25 // Make sure the file is readable
26 if ( ! (is_file($filename) AND is_readable($filename)))
29 // Get the extension from the filename
30 $extension = strtolower(substr(strrchr($filename, '.'), 1));
32 if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension))
34 // Disable error reporting
35 $ER = error_reporting(0);
37 // Use getimagesize() to find the mime type on images
38 $mime = getimagesize($filename);
40 // Turn error reporting back on
43 // Return the mime type
44 if (isset($mime['mime']))
48 if (function_exists('finfo_open'))
50 // Use the fileinfo extension
51 $finfo = finfo_open(FILEINFO_MIME
);
52 $mime = finfo_file($finfo, $filename);
55 // Return the mime type
59 if (ini_get('mime_magic.magicfile') AND function_exists('mime_content_type'))
61 // Return the mime type using mime_content_type
62 return mime_content_type($filename);
67 // Attempt to locate use the file command, checking the return value
68 if ($command = trim(exec('which file', $output, $return)) AND $return === 0)
70 return trim(exec($command.' -bi '.escapeshellarg($filename)));
74 if ( ! empty($extension) AND is_array($mime = Kohana
::config('mimes.'.$extension)))
76 // Return the mime-type guess, based on the extension
80 // Unable to find the mime-type
85 * Split a file into pieces matching a specific size.
87 * @param string file to be split
88 * @param string directory to output to, defaults to the same directory as the file
89 * @param integer size, in MB, for each chunk to be
90 * @return integer The number of pieces that were created.
92 public static function split($filename, $output_dir = FALSE, $piece_size = 10)
95 $output_dir = ($output_dir == FALSE) ?
pathinfo(str_replace('\\', '/', realpath($filename)), PATHINFO_DIRNAME
) : str_replace('\\', '/', realpath($output_dir));
96 $output_dir = rtrim($output_dir, '/').'/';
98 // Open files for writing
99 $input_file = fopen($filename, 'rb');
101 // Change the piece size to bytes
102 $piece_size = 1024 * 1024 * (int) $piece_size; // Size in bytes
104 // Set up reading variables
105 $read = 0; // Number of bytes read
106 $piece = 1; // Current piece
107 $chunk = 1024 * 8; // Chunk size to read
110 while ( ! feof($input_file))
113 $piece_name = $filename.'.'.str_pad($piece, 3, '0', STR_PAD_LEFT
);
114 $piece_open = @fopen
($piece_name, 'wb+') or die('Could not write piece '.$piece_name);
116 // Fill the current piece
117 while ($read < $piece_size AND $data = fread($input_file, $chunk))
119 fwrite($piece_open, $data) or die('Could not write to open piece '.$piece_name);
123 // Close the current piece
126 // Prepare to open a new piece
130 // Make sure that piece is valid
131 ($piece < 999) or die('Maximum of 999 pieces exceeded, try a larger piece size');
137 // Returns the number of pieces that were created
142 * Join a split file into a whole file.
144 * @param string split filename, without .000 extension
145 * @param string output filename, if different then an the filename
146 * @return integer The number of pieces that were joined.
148 public static function join($filename, $output = FALSE)
150 if ($output == FALSE)
153 // Set up reading variables
154 $piece = 1; // Current piece
155 $chunk = 1024 * 8; // Chunk size to read
158 $output_file = @fopen
($output, 'wb+') or die('Could not open output file '.$output);
161 while ($piece_open = @fopen
(($piece_name = $filename.'.'.str_pad($piece, 3, '0', STR_PAD_LEFT
)), 'rb'))
163 // Write the piece into the output file
164 while ( ! feof($piece_open))
166 fwrite($output_file, fread($piece_open, $chunk));
169 // Close the current piece
172 // Prepare for a new piece
175 // Make sure piece is valid
176 ($piece < 999) or die('Maximum of 999 pieces exceeded');
179 // Close the output file
180 fclose($output_file);
182 // Return the number of pieces joined