Initial Kohana install
[speedfreak] / Server / system / helpers / file.php
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         /**
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.
18          *
19          * @param   string   filename
20          * @return  string   mime-type, if found
21          * @return  boolean  FALSE, if not found
22          */
23         public static function mime($filename)
24         {
25                 // Make sure the file is readable
26                 if ( ! (is_file($filename) AND is_readable($filename)))
27                         return FALSE;
28
29                 // Get the extension from the filename
30                 $extension = strtolower(substr(strrchr($filename, '.'), 1));
31
32                 if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension))
33                 {
34                         // Disable error reporting
35                         $ER = error_reporting(0);
36
37                         // Use getimagesize() to find the mime type on images
38                         $mime = getimagesize($filename);
39
40                         // Turn error reporting back on
41                         error_reporting($ER);
42
43                         // Return the mime type
44                         if (isset($mime['mime']))
45                                 return $mime['mime'];
46                 }
47
48                 if (function_exists('finfo_open'))
49                 {
50                         // Use the fileinfo extension
51                         $finfo = finfo_open(FILEINFO_MIME);
52                         $mime  = finfo_file($finfo, $filename);
53                         finfo_close($finfo);
54
55                         // Return the mime type
56                         return $mime;
57                 }
58
59                 if (ini_get('mime_magic.magicfile') AND function_exists('mime_content_type'))
60                 {
61                         // Return the mime type using mime_content_type
62                         return mime_content_type($filename);
63                 }
64
65                 if ( ! KOHANA_IS_WIN)
66                 {
67                         // Attempt to locate use the file command, checking the return value
68                         if ($command = trim(exec('which file', $output, $return)) AND $return === 0)
69                         {
70                                 return trim(exec($command.' -bi '.escapeshellarg($filename)));
71                         }
72                 }
73
74                 if ( ! empty($extension) AND is_array($mime = Kohana::config('mimes.'.$extension)))
75                 {
76                         // Return the mime-type guess, based on the extension
77                         return $mime[0];
78                 }
79
80                 // Unable to find the mime-type
81                 return FALSE;
82         }
83
84         /**
85          * Split a file into pieces matching a specific size.
86          *
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.
91          */
92         public static function split($filename, $output_dir = FALSE, $piece_size = 10)
93         {
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, '/').'/';
97
98                 // Open files for writing
99                 $input_file = fopen($filename, 'rb');
100
101                 // Change the piece size to bytes
102                 $piece_size = 1024 * 1024 * (int) $piece_size; // Size in bytes
103
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
108
109                 // Split the file
110                 while ( ! feof($input_file))
111                 {
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);
115
116                         // Fill the current piece
117                         while ($read < $piece_size AND $data = fread($input_file, $chunk))
118                         {
119                                 fwrite($piece_open, $data) or die('Could not write to open piece '.$piece_name);
120                                 $read += $chunk;
121                         }
122
123                         // Close the current piece
124                         fclose($piece_open);
125
126                         // Prepare to open a new piece
127                         $read = 0;
128                         $piece++;
129
130                         // Make sure that piece is valid
131                         ($piece < 999) or die('Maximum of 999 pieces exceeded, try a larger piece size');
132                 }
133
134                 // Close input file
135                 fclose($input_file);
136
137                 // Returns the number of pieces that were created
138                 return ($piece - 1);
139         }
140
141         /**
142          * Join a split file into a whole file.
143          *
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.
147          */
148         public static function join($filename, $output = FALSE)
149         {
150                 if ($output == FALSE)
151                         $output = $filename;
152
153                 // Set up reading variables
154                 $piece = 1; // Current piece
155                 $chunk = 1024 * 8; // Chunk size to read
156
157                 // Open output file
158                 $output_file = @fopen($output, 'wb+') or die('Could not open output file '.$output);
159
160                 // Read each piece
161                 while ($piece_open = @fopen(($piece_name = $filename.'.'.str_pad($piece, 3, '0', STR_PAD_LEFT)), 'rb'))
162                 {
163                         // Write the piece into the output file
164                         while ( ! feof($piece_open))
165                         {
166                                 fwrite($output_file, fread($piece_open, $chunk));
167                         }
168
169                         // Close the current piece
170                         fclose($piece_open);
171
172                         // Prepare for a new piece
173                         $piece++;
174
175                         // Make sure piece is valid
176                         ($piece < 999) or die('Maximum of 999 pieces exceeded');
177                 }
178
179                 // Close the output file
180                 fclose($output_file);
181
182                 // Return the number of pieces joined
183                 return ($piece - 1);
184         }
185
186 } // End file