forked from opendocman/opendocman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile_class.php
228 lines (191 loc) · 6.57 KB
/
File_class.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* File helper class.
*
* @category Helpers
* @author Kohana Team
* @copyright (c) 2007-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class File
{
/**
* 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.
*
* $mime = File::mime($file);
*
* @param string $filepath file name or path
* @param string $realname The real filename of the file (without the path)
* @return string mime type on success
* @return FALSE on failure
*/
public static function mime($filename, $realname)
{
// Get the complete path to the file
$filename = realpath($filename);
// Get the extension from the filename
$extension = strtolower(pathinfo($realname, PATHINFO_EXTENSION));
if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension)) {
// Use getimagesize() to find the mime type on images
$file = getimagesize($filename);
if (isset($file['mime']))
return $file['mime'];
}
// First lets try finfo
if (class_exists('finfo')) {
if ($info = new finfo(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME)) {
return $info->file($filename);
} else if ($info = new finfo(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME, 'magic')) {
return $info->file($filename);
}
}
// No finfo, so lets try mime_content_type
if (function_exists('mime_content_type')) {
$mimetype = mime_content_type($filename);
if($mimetype) {
return $mimetype;
}
}
// Nothing else has worked,lets try the mimetypes global array
if (!empty($extension)) {
return File::mime_by_ext($extension);
}
// Unable to find the mime-type
return FALSE;
}
/**
* Return the mime type of an extension.
*
* $mime = File::mime_by_ext('png'); // "image/png"
*
* @param string $extension php, pdf, txt, etc
* @return string mime type on success
* @return FALSE on failure
*/
public static function mime_by_ext($extension)
{
$return = isset($GLOBALS['mimetypes'][$extension]) ? $GLOBALS['mimetypes'][$extension][0] : FALSE;
return $return;
}
/**
* Lookup MIME types for a file
*
* @see Kohana_File::mime_by_ext()
* @param string $extension Extension to lookup
* @return array Array of MIMEs associated with the specified extension
*/
public static function mimes_by_ext($extension)
{
return isset($GLOBALS['mimetypes'][$extension]) ? ( (array) $GLOBALS['mimetypes'][$extension]) : array();
}
/**
* Lookup file extensions by MIME type
*
* @param string $type File MIME type
* @return array File extensions matching MIME type
*/
public static function exts_by_mime($type)
{
static $types = array();
// Fill the static array
if (empty($types)) {
foreach ($GLOBALS['mimetypes'] as $ext => $mimes) {
foreach ($mimes as $mime) {
if ($mime == 'application/octet-stream') {
// octet-stream is a generic binary
continue;
}
if (!isset($types[$mime])) {
$types[$mime] = array((string) $ext);
} elseif (!in_array($ext, $types[$mime])) {
$types[$mime][] = (string) $ext;
}
}
}
}
return isset($types[$type]) ? $types[$type] : FALSE;
}
/**
* Lookup a single file extension by MIME type.
*
* @param string $type MIME type to lookup
* @return mixed First file extension matching or false
*/
public static function ext_by_mime($type)
{
return current(File::exts_by_mime($type));
}
/**
* Split a file into pieces matching a specific size. Used when you need to
* split large files into smaller pieces for easy transmission.
*
* $count = File::split($file);
*
* @param string $filename file to be split
* @param integer $piece_size size, in MB, for each piece to be
* @return integer The number of pieces that were created
*/
public static function split($filename, $piece_size = 10)
{
// Open the input file
$file = fopen($filename, 'rb');
// Change the piece size to bytes
$piece_size = floor($piece_size * 1024 * 1024);
// Write files in 8k blocks
$block_size = 1024 * 8;
// Total number of peices
$peices = 0;
while (!feof($file)) {
// Create another piece
$peices += 1;
// Create a new file piece
$piece = str_pad($peices, 3, '0', STR_PAD_LEFT);
$piece = fopen($filename . '.' . $piece, 'wb+');
// Number of bytes read
$read = 0;
do {
// Transfer the data in blocks
fwrite($piece, fread($file, $block_size));
// Another block has been read
$read += $block_size;
} while ($read < $piece_size);
// Close the piece
fclose($piece);
}
// Close the file
fclose($file);
return $peices;
}
/**
* Join a split file into a whole file. Does the reverse of [File::split].
*
* $count = File::join($file);
*
* @param string $filename split filename, without .000 extension
* @return integer The number of pieces that were joined.
*/
public static function join($filename)
{
// Open the file
$file = fopen($filename, 'wb+');
// Read files in 8k blocks
$block_size = 1024 * 8;
// Total number of peices
$pieces = 0;
while (is_file($piece = $filename . '.' . str_pad($pieces + 1, 3, '0', STR_PAD_LEFT))) {
// Read another piece
$pieces += 1;
// Open the piece for reading
$piece = fopen($piece, 'rb');
while (!feof($piece)) {
// Transfer the data in blocks
fwrite($file, fread($piece, $block_size));
}
// Close the peice
fclose($piece);
}
return $pieces;
}
}