Merge branch 'maint/7.0'
[ninja.git] / system / helpers / file.php
blobfe3fb8c3336c74c0f8fc7626e209a0c5338dbdb7
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * File helper class.
5 * $Id: file.php 3917 2009-01-21 03:06:22Z zombor $
7 * @package Core
8 * @author Kohana Team
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class file {
14 /**
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)))
27 return FALSE;
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
41 error_reporting($ER);
43 // Return the mime type
44 if (isset($mime['mime']))
45 return $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);
53 finfo_close($finfo);
55 // Return the mime type
56 return $mime;
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);
65 if ( ! KOHANA_IS_WIN)
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
77 return $mime[0];
80 // Unable to find the mime-type
81 return FALSE;
84 /**
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)
94 // Find output dir
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
109 // Split the file
110 while ( ! feof($input_file))
112 // Open a new piece
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);
120 $read += $chunk;
123 // Close the current piece
124 fclose($piece_open);
126 // Prepare to open a new piece
127 $read = 0;
128 $piece++;
130 // Make sure that piece is valid
131 ($piece < 999) or die('Maximum of 999 pieces exceeded, try a larger piece size');
134 // Close input file
135 fclose($input_file);
137 // Returns the number of pieces that were created
138 return ($piece - 1);
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)
151 $output = $filename;
153 // Set up reading variables
154 $piece = 1; // Current piece
155 $chunk = 1024 * 8; // Chunk size to read
157 // Open output file
158 $output_file = @fopen($output, 'wb+') or die('Could not open output file '.$output);
160 // Read each piece
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
170 fclose($piece_open);
172 // Prepare for a new piece
173 $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
183 return ($piece - 1);
186 } // End file