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