-
Notifications
You must be signed in to change notification settings - Fork 6
/
file.php
246 lines (210 loc) · 5.03 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
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
<?php
function dir_list_files($path){
if (!is_dir($path)){
return [];
}
$dir = opendir($path);
$files = [];
if (!empty($dir)){
while (false!==($file = readdir($dir))){
if ($file!='.' && $file!='..'){
$files[] = $file;
}
}
closedir($dir);
sort($files);
}
return $files;
}
function dir_list_recursive($path, $list = [], $prefix = ''){
$files = dir_list_files($path);
foreach ($files as $file){
$file_path = $path.'/'.$file;
if (is_dir($file_path)){
$list = dir_list_recursive($file_path, $list, $prefix.$file.'/');
}
else {
$list[] = $prefix.$file;
}
}
sort($list);
return $list;
}
function dir_tree($dir, $root = null){
foreach ($dir as $name => $sub){
$name = $root.$name;
if (!file_exists($name)){
mkdir($name);
}
if (is_array($sub)){
dir_tree($sub, $name.'/');
}
}
return true;
}
function file_delete($file){
if (!file_exists($file)){
return true;
}
if (!is_writable($file)){
return false;
}
$fh = fopen($file, 'w');
if (empty($fh)){
return false;
}
fclose($fh);
unlink($file);
return true;
}
function file_load($file, $serialize = false){
if (!is_readable($file)){
return false;
}
$size = filesize($file);
if (empty($size)){
return '';
}
$fh = fopen($file, 'r');
if (!empty($fh)){
$string = fread($fh, $size);
fclose($fh);
}
return $serialize ? unserialize($string) : $string;
}
function file_get_json($url, $throw_on_error = true){
if (substr($url, 0, 4)!='http'){
$url = 'http://'.$url;
}
$string = file_get_contents($url);
return file_parse_json($string, $throw_on_error);
}
function file_parse_json($string, $throw_on_error = true){
if (empty($string)){
return [];
}
$json = json_decode($string, true);
if ($json===null){
if ($throw_on_error){
throw new \Exception('The JSON could not be parsed correctly: "'.json_error_msg().'"');
}
$json = [];
}
return $json;
}
function file_load_json($file, $throw_on_error = true){
$string = file_load($file);
return file_parse_json($string, $throw_on_error);
}
// deprecated
function get_file($filename){
return file_load($filename);
}
function file_output($file_path, $mime = null, $file_name = null){
if (empty($file_name)){
$file_name = substr_after($file_path, '/');
}
header('Content-type: '.$mime);
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header('Content-Transfer-Encoding: binary');
// header('Content-Length: '.filesize(DIR.$summary_pdf));
header('Accept-Ranges: bytes');
ob_clean();
flush();
readfile($file_path);
}
function file_save($file, $string, $overwrite = false){
if (is_array($string)){
$string = serialize($string);
}
return file_save_($file, $string, $overwrite);
}
function file_save_json($file, $string, $overwrite = true, $pretty = false){
if (is_array($string)){
$string = json_encode($string, $pretty);
}
return file_save_($file, $string, $overwrite);
}
function file_save_($file, $string, $overwrite){
if (file_exists($file) && !is_writable($file)){
return false;
}
$fh = @fopen($file, $overwrite ? 'w' : 'a');
if (empty($fh)){
return false;
}
fwrite($fh, $string);
fclose($fh);
return true;
}
function file_store($id, $params, &$error = null){
$out = $params['out'].$id.(!empty($params['ext']) ? '.'.$params['ext'] : '');
if (!copy($params['in'], $out)){
$error = 'The file was uploaded but could not be stored. The file "'.$params['in'].'" could not be copied to "'.$out.'".';
return false;
}
return true;
}
function file_types($check, $file_types = null){
if (empty($file_types)){
$file_types = ['pdf', 'doc', 'xls', 'ppt', 'docx', 'xlsx', 'pptx', 'mp3', 'jpeg', 'jpg', 'png', 'avi', 'mov', 'mpg', 'mpeg', 'mp4', '3gs', 'wmv', 'asx'];
}
return in_array($check, $file_types);
}
function file_valid($file, &$error, $file_types = null){
$new_file['temp'] = $file['tmp_name'];
$dot = strrpos($file['name'], '.');
$new_file['ext'] = strtolower(substr($file['name'], $dot+1));
if (file_types($new_file['ext'], $file_types)){
return $new_file;
}
else {
$error = 'The file you have uploaded is the wrong file type ('.$new_file['ext'].').';
return false;
}
}
function folder_copy($src, $dst, $recurse = true){
$dir = opendir($src);
if (!empty($dir)){
@mkdir($dst);
while (false!==($file = readdir($dir))){
if ($file!='.' && $file!='..'){
if (is_dir($src.'/'.$file)){
if ($recurse){
folder_copy($src.'/'.$file, $dst.'/'.$file);
}
}
else {
copy($src.'/'.$file, $dst.'/'.$file);
}
}
}
closedir($dir);
}
}
// Taken from http://stackoverflow.com/a/22500394/518703
function php_size_to_bytes($sSize){
if (is_numeric($sSize)){
return $sSize;
}
$sSuffix = substr($sSize, -1);
$iValue = substr($sSize, 0, -1);
switch (strtoupper($sSuffix)){
case 'P':
$iValue *= 1024;
case 'T':
$iValue *= 1024;
case 'G':
$iValue *= 1024;
case 'M':
$iValue *= 1024;
case 'K':
$iValue *= 1024;
break;
}
return $iValue;
}
function get_max_upload_size(){
return min(php_size_to_bytes(ini_get('post_max_size')), php_size_to_bytes(ini_get('upload_max_filesize')));
}
//