1: <?php defined('_JOOS_CORE') or exit();
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18: class joosFile
19: {
20: 21: 22: 23: 24: 25: 26: 27: 28:
29: public static function convert_size($num)
30: {
31: $num = (double) $num;
32:
33: if ($num >= 1073741824) {
34: $num = round($num / 1073741824 * 100) / 100 . ' ' . 'gb';
35: } elseif ($num >= 1048576) {
36: $num = round($num / 1048576 * 100) / 100 . ' ' . 'mb';
37: } elseif ($num >= 1024) {
38: $num = round($num / 1024 * 100) / 100 . ' ' . 'kb';
39: } else {
40: $num .= ' ' . 'byte';
41: }
42:
43: return $num;
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: public static function create($file_name, $data)
54: {
55: return file_put_contents($file_name, $data, LOCK_EX);
56: }
57:
58: 59: 60: 61: 62: 63: 64: 65: 66:
67: public static function delete($file_name)
68: {
69: self::exception_if_file_not_exists($file_name);
70:
71: return unlink((string) $file_name);
72: }
73:
74: 75: 76: 77: 78: 79: 80: 81: 82:
83: public static function exists($file_name)
84: {
85: return (bool) (file_exists($file_name) && is_file($file_name));
86: }
87:
88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98:
99: public static function get_mime_content_type($file_name)
100: {
101: if (function_exists('finfo_open')) {
102: $options = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
103: $info = finfo_open($options);
104:
105: if ($info && ($result = finfo_file($info, $file_name)) !== false) {
106: return $result;
107: }
108: }
109:
110: if (function_exists('mime_content_type') && ($result = mime_content_type($file_name)) !== false) {
111: return $result;
112: }
113:
114: return self::get_mime_content_type_by_extension($file_name);
115:
116: }
117:
118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128:
129: public static function get_mime_content_type_by_extension($file_name)
130: {
131: $mime_types = array(
132: 'txt' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html', 'php' => 'text/html', 'css' => 'text/css', 'js' => 'application/javascript', 'json' => 'application/json', 'xml' => 'application/xml', 'swf' => 'application/x-shockwave-flash', 'flv' => 'video/x-flv', 'sql' => 'text/x-sql',
133: 'png' => 'image/png', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'gif' => 'image/gif', 'bmp' => 'image/bmp', 'ico' => 'image/vnd.microsoft.icon', 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml', 'tga' => 'image/x-targa', 'psd' => 'image/vnd.adobe.photoshop',
134: 'zip' => 'application/zip', 'rar' => 'application/x-rar-compressed', 'exe' => 'application/x-msdownload', 'msi' => 'application/x-msdownload', 'cab' => 'application/vnd.ms-cab-compressed', 'gz' => 'application/x-gzip', 'tgz' => 'application/x-gzip', 'bz' => 'application/x-bzip2', 'bz2' => 'application/x-bzip2', 'tbz' => 'application/x-bzip2', 'tar' => 'application/x-tar', '7z' => 'application/x-7z-compressed',
135: 'mp3' => 'audio/mpeg', 'qt' => 'video/quicktime', 'mov' => 'video/quicktime', 'avi' => 'video/x-msvideo', 'dv' => 'video/x-dv', 'mp4' => 'video/mp4', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'wm' => 'video/x-ms-wmv', 'mkv' => 'video/x-matroska',
136: 'pdf' => 'application/pdf', 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'ps' => 'application/postscript',
137: 'doc' => 'application/msword', 'rtf' => 'application/rtf', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint',
138: 'odt' => 'application/vnd.oasis.opendocument.text', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet');
139:
140: $file_info = pathinfo($file_name);
141: $ext = $file_info['extension'];
142: if (isset($mime_types[$ext])) {
143: return $mime_types[$ext];
144: } else {
145: return 'application/octet-stream';
146: }
147: }
148:
149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161:
162: public static function make_file_location($id, $split_by = 3, $capacity = 9, $round = false)
163: {
164:
165: $id = $round ? (int) round($id / $round) : $id;
166:
167: if (!is_integer($id)) {
168: throw new joosFileLibrariesException('Параметр $id должен иметь цельночисленное значение');
169: }
170:
171: if (!is_integer($split_by)) {
172: throw new joosFileLibrariesException('Параметр $split_by должен иметь цельночисленное значение');
173: }
174:
175: if (!is_integer($capacity)) {
176: throw new joosFileLibrariesException('Параметр $capacity должен иметь цельночисленное значение');
177: }
178:
179: $p = sprintf('%0' . $capacity . 'd', $id);
180: $h = str_split($p, $split_by);
181:
182: return implode('/', $h);
183: }
184:
185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201:
202: public static function file_info($file_name)
203: {
204: self::exception_if_file_not_exists($file_name);
205:
206: $f = pathinfo($file_name);
207:
208: $r = array();
209: $r['mime'] = self::get_mime_content_type($file_name);
210: $r['size'] = filesize($file_name);
211: $r['ext'] = isset($f['extension']) ? $f['extension'] : '';
212: $r['name'] = $f['basename'];
213:
214: return $r;
215: }
216:
217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227:
228: public static function make_safe_name($file_name)
229: {
230:
231: $file_name = str_ireplace(array('ь', 'ъ'), '', $file_name);
232:
233: $file_name = joosText::russian_transliterate($file_name);
234:
235: $file_name = strtolower($file_name);
236:
237: $file_name = str_replace(array("'", '-'), ' ', $file_name);
238: $file_name = preg_replace('/[^\-a-z0-9\._]+/u', '-', $file_name);
239:
240: return trim($file_name, '-');
241: }
242:
243: 244: 245: 246: 247: 248: 249:
250: public static function get_modified_date($file_name)
251: {
252: self::exception_if_file_not_exists($file_name);
253:
254: return filemtime($file_name);
255: }
256:
257: 258: 259: 260: 261: 262: 263:
264: public static function get_size($file_name)
265: {
266: self::exception_if_file_not_exists($file_name);
267:
268: return filesize($file_name);
269: }
270:
271: 272: 273: 274: 275: 276: 277:
278: public static function get_type($file_name)
279: {
280: self::exception_if_file_not_exists($file_name);
281:
282: return filetype($file_name);
283: }
284:
285: 286: 287: 288: 289: 290: 291: 292:
293: public static function put_content($file_name, $data)
294: {
295: self::exception_if_file_not_exists($file_name);
296:
297: if (!joosFile::is_writable($file_name)) {
298: throw new joosFileLibrariesException('Файл :file не доступен для записи', array(':file' => $file_name));
299: }
300:
301: return file_put_contents($file_name, $data, LOCK_EX);
302: }
303:
304: 305: 306: 307: 308: 309: 310:
311: public static function get_content($file_name)
312: {
313: self::exception_if_file_not_exists($file_name);
314:
315: if (!joosFile::is_writable($file_name)) {
316: throw new joosFileLibrariesException('Файл :file не доступен для чтения', array(':file' => $file_name));
317: }
318:
319: return file_get_contents($file_name);
320: }
321:
322: 323: 324: 325: 326: 327: 328:
329: public static function is_writable($file_location)
330: {
331: return (bool) is_writable($file_location);
332: }
333:
334: 335: 336: 337: 338: 339: 340:
341: public static function is_readable($file_location)
342: {
343: return (bool) is_readable($file_location);
344: }
345:
346: 347: 348: 349: 350: 351: 352: 353:
354: private static function exception_if_file_not_exists($file_name)
355: {
356: if (!joosFile::exists($file_name)) {
357: throw new joosFileLibrariesException('Файл :file не существует', array(':file' => $file_name));
358: }
359: }
360: }
361:
362: 363: 364:
365: class joosFileLibrariesException extends joosException
366: {
367: }
368: