-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.php
161 lines (149 loc) · 5.19 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
<?php
/**
* Create and manage files.
*
* @copyright 2010 Artur Gajewski
* @license http://framework.zend.com/license BSD License
* @version Release: @package_version@
* @link http://ztools.arturgajewski.com
* @since Class available since Release 1.2.3
*/
class Ztools_File
{
protected static $_mimeTypes = array(
'css' => 'text/css',
'doc' => 'application/msword',
'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'dot' => 'application/msword',
'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'dtd' => 'text/dtd',
'eps' => 'application/postscript',
'gif' => 'image/gif',
'htm' => 'text/htm',
'html' => 'text/html',
'ico' => 'image/x-icon',
'ifd' => 'application/octet-stream', # Jet Form
'jpe' => 'image/jpe',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'pdf' => 'application/pdf',
'png' => 'image/png',
'properties' => 'application/octet-stream',
'mp3' => 'audio/mpeg',
'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
'pps' => 'application/vnd.ms-powerpoint',
'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
'ppt' => 'application/vnd.ms-powerpoint',
'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'ps' => 'application/postscript',
'rtf' => 'application/rtf',
'sgm' => 'text/sgml',
'sgml' => 'text/sgml',
'swf' => 'application/x-shockwave-flash',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'txt' => 'text/plain',
'xhtml' => 'text/html',
'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
'xls' => 'application/vnd.ms-excel',
'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
'xml' => 'text/xml',
'zip' => 'application/zip');
/**
* Write contents to a file
* @param $filename
* @param $contents
*/
static function write( $filename, $contents )
{
file_put_contents($filename, $contents);
}
/**
* Get contents of a file
* @param string $filename
* @return mixed $contents
*/
static function read( $filename )
{
return file_get_contents($filename);
}
/**
* Get contents of a file and split lines into array
* @param string $filename
* @return array
*/
static function readToArray( $filename )
{
$contents = file_get_contents($filename);
return explode("\n", $contents);
}
/**
* Check if a file exists
* @param string $filename
* @return boolean
*/
static function exists( $filename )
{
return is_file($filename);
}
/**
* Delete a file
* @param string $filename
*/
static function delete( $filename )
{
if (file_exists( $filename )) {
return unlink($filename);
}
}
/**
* Create a blank file
* @param $filename
*/
static function touch( $filename )
{
file_put_contents($filename, null);
}
/**
* Highlight given file with PHP syntax
* @param string $filename
* @param boolean $return
* @return string or null
*/
static function highlight( $filename )
{
return highlight_file($filename, true);
}
/**
* Obtain extension of a filename
* @param string $filename
* @return string
*/
static function getExtension( $filename )
{
return end(explode(".", $filename));
}
/**
* Obtain mimetype of a filename
* @param string $filename
* @return string
*/
static function getMimeType( $filename )
{
$extension = self::getExtension( $filename );
if (!isset(self::$_mimeTypes[$extension])) {
throw new Ztools_File_Exception('Unknown extension: ' . $filename);
}
return self::$_mimeTypes[$extension];
}
}