forked from surebert/surebert-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiles.php
295 lines (246 loc) · 6.54 KB
/
Files.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
* Various functions for working with files
* @author Paul Visco
* @version 1.3 11-19-07 08-18-09
* @package sb_Files
*
*/
class sb_Files{
/**
* read a file into chunks for faster force download and better memory management
*
*/
public static function read_chunked($file_name,$seekat=0,$return_bytes=true) {
// how many bytes per chunk
$chunk_size = 1*(1024*1024);
$buffer = '';
$cnt =0;
$handle = fopen($file_name, 'rb');
fseek($handle, $seekat);
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunk_size);
echo $buffer;
if(ob_get_level()){
ob_flush();
flush();
}
if ($return_bytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($return_bytes && $status) {
return $cnt;
}
return $status;
}
/**
* Used to convert file extensions to the appropriate mime-type
* http://www.ltsw.se/knbase/internet/mime.htp
* @param string $ext e.g. mp3
* @return mixed boolean/string e.g. audio/mpeg, false if not found
*/
public static function extension_to_mime($ext){
switch($ext){
case 'bmp':
case 'gif':
case 'jpg':
case 'jpeg':
case 'png':
case 'tif':
$ext = ($ext=='jpg') ? 'jpeg' : $ext;
$m = 'image/'.$ext;
break;
case 'js':
case 'json':
case 'rtf':
case 'pdf':
case 'xml':
$m = 'application/'.$ext;
break;
case 'html':
case 'txt':
case 'css':
case 'csv':
case 'php':
$ext = $ext == 'txt' ? 'plain' : $ext;
$m = 'text/'.$ext;
break;
case 'doc':
$m = 'application/msword';
break;
case 'docx':
$m = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case 'flv':
$m = 'video/x-flv';
break;
case 'gz':
$m = 'application/x-gzip';
break;
case 'mp3':
$m = 'audio/mpeg';
break;
case 'mp4':
$m = 'video/mp4';
break;
case 'mid':
$m = 'audio/x-midi';
break;
case 'ppt':
case 'pps':
case 'ppsx':
$m = 'application/vnd.ms-powerpoint';
break;
case 'tar':
$m = 'application/x-tar';
break;
case 'wav':
$m = 'audio/x-wav';
break;
case 'xls':
$m = 'application/vnd.ms-excel';
break;
case 'zip':
$m = 'application/x-zip-compressed';
break;
default:
$m = false;
}
return $m;
}
/**
* Gets mime type from file from finfo ext
*
* @param string $file Path to file
* @return string The mime type from finfo
*/
public static function file_to_mime($file){
$mime = self::filename_to_mime($file);
if($mime){
return $mime;
} else if(class_exists('finfo') && is_file($file)){
$finfo = @new finfo(FILEINFO_MIME, "/usr/share/misc/magic");
if($finfo){
/* get mime-type for a specific file */
return $finfo->file($file);
}
}
return false;
}
/**
* Convert a filename to a mime type
* @param string $filename
* @return string The mime type of the file
*/
public static function filename_to_mime($filename){
$arr = explode(".", basename($filename));
$ext = strtolower(end($arr));
return self::extension_to_mime($ext);
}
/**
* Recursively deletes the files in a diretory
*
* @param string $dir The directory path
* @param boolean $del Should directory itself be deleted upon completion
* @return boolean
*/
public static function recursive_delete($dir, $del=0){
if($dir == '/'){
die("You cannot delete the root directory");
}
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file){
$name = $file->getFilename();
if ($file->isDir() && $name != '.' && $name != '..') {
rmdir($file->getPathname());
} else if($file->isFile()){
unlink($file->getPathname());
}
}
if($del ==1){
rmdir($dir);
}
}
/**
* Determines the size of directors and returns stats object
* @param string $path The path to the server
* @return object
*/
public static function get_directory_size($path) {
$stats = new stdClass();
$stats->size = 0;
$stats->file_count = 0;
$stats->dir_count = 0;
if ($handle = opendir ($path)) {
while (false !== ($file = readdir($handle))) {
$nextpath = $path . '/' . $file;
if ($file != '.' && $file != '..' && !is_link ($nextpath)) {
if (is_dir ($nextpath)) {
$stats->dir_count++;
$result = self::get_directory_size($nextpath);
$stats->size += $result->size;
$stats->file_count += $result->file_count;
$stats->dir_count += $result->dir_count;
}
elseif (is_file ($nextpath)) {
$stats->size += filesize ($nextpath);
$stats->file_count++;
}
}
}
}
closedir ($handle);
return $stats;
}
/**
* Get the files from a directory
* @param string $dir The directory path
* @param boolean $get_directories Should subdirectories be listed
* @return array
*/
public static function get_files($dir, $get_directories=false){
$arr = Array();
$iterator = new DirectoryIterator($dir);
foreach ($iterator as $file){
if ($get_directories && $file->isDir() && !$file->isDot() && !preg_match("~\.~", $file)) {
$arr[$file->getBasename()] = Array(
'path' => $file->getPath(),
'size' => self::get_directory_size($file->getRealPath()),
'mtime' => $file->getMTime(),
'name' => $file->getBaseName());
} else if (!$get_directories && $file->isFile()){
$arr[] = $file->getBasename();
}
}
$get_directories ? ksort($arr) : sort($arr);
return $arr;
}
/**
* Converts file size to string that is human readible
* @param integer $size The size in bytes
* @return string
*/
public static function size_to_string($size) {
if($size<1024) {
return $size." bytes";
}
else if($size<(1024*1024)) {
$size=round($size/1024,1);
return $size." KB";
}
else if($size<(1024*1024*1024)) {
$size=round($size/(1024*1024),1);
return $size." MB";
}
else {
$size=round($size/(1024*1024*1024),1);
return $size." GB";
}
}
}
?>