class_mcFile.inc.php
5.61 KB
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
<?php
/**
* @copyright (c) 2014, netz.coop eG
*/
class mcFile {
/**
*
*
* @param string $param_filename
* @param string $param_text
* @param string $param_kindof -- r|r+|w|w+|a|a+ @see http://de3.php.net/manual/de/function.fopen.php
* r - ptr at the beginning
* w - ptr at the beginning and file size 0
* a - ptr at the end
* @return bool
*/
public static function write($param_filename, $param_text, $param_kindof = null) {
if($param_filename && $param_text) {
if($param_kindof === null) {
$param_kindof = 'a';
}
try {
@$var_file = fopen($param_filename,$param_kindof);
if ($var_file === false) {
return false;
} else {
fputs($var_file, $param_text);
// fputs($var_file, mcCONST::LINEBREAK.mcCONST::LINEBREAK);
}
fclose($var_file);
return true;
} catch (Exception $e) {
mcException::handleException($e);
}
} else {
return false;
}
}
public static function getFileContent($param_filename, $param_asString = false) {
if(is_readable($param_filename)) {
try {
$handle = fopen ($param_filename, "r");
$array_content = array();
while (!feof($handle)) {
$array_content[] = fgets($handle);
}
fclose ($handle);
if($param_asString) {
$filecontent_string = '';
foreach($array_content as $line) {
$filecontent_string .= $line;
}
return $filecontent_string;
} else {
return $array_content;
}
} catch (Exception $e) {
mcException::handleException($e);
return false;
}
} else {
return Error::newError('DevError', 'file could not read', $param_code);
}
}
public static function deleteFile($param_filename) {
D::ulli("mcFile::deleteFile($param_filename)");
return unlink($param_filename);
}
public static function rename($param_from, $param_to){
//file_exists() - Existenz der Datei prüfen
if(!file_exists($param_to)){
return rename($param_from, $param_to);
// //copy() - Datei kopieren
// if (!copy($from, $to)) {
// print ("failed to copy $file...<br>\n");
// }
// else{
// //unlink() - Datei löschen
// unlink($from);
// }
} else {
return false;
}
}
/**
* erstellt eine temporäre Datei, für weitere Dateimanipulatioin
*
* @param array $param_outputfile @see mcFile::manipulateOutputfileArray($param_outputfile)
* @return string
*/
public static function createTmpFile(array $param_outputfile=null, $param_filepath=null) {
if(is_null($param_outputfile)) {
$param_outputfile = array();
}
mcFile::manipulateOutputfileArray($param_outputfile, $param_filepath);
$param_file = tempnam($param_outputfile['dirname'], $param_outputfile['prefix']);
if($param_outputfile['filenameextension']) {
$tmp_filename = $param_file.$param_outputfile['filenameextension'];
// @todo tempfile auch noch mal überprüfen, das es nicht existiert
mcFile::rename($param_file, $tmp_filename);
}
return $tmp_filename;
}
/**
* manipuliert $param_outputfile so das Informationen für die zuerstellende Datei
* für Dateimanipulationen vorliegen
*
* @todo es könnte überprüft werden ob $param_outputfile['dirname'] schreibbar ist
* @todo es könnte überprüft werden ob $param_outputfile['filename'] schreibbar ist
* @todo filepath könnte alle anderen Werte generieren
* @todo filenameextension muessste extension heissen, so wie bei pathinfo()
*
* @param array $param_outputfile - array('dirname'=>,'prefix'=>,'filename'=>,'filenameextension'=>, )
* @param type $param_filepath - generiert bis jetzt nur dirname
*/
public static function manipulateOutputfileArray(array &$param_outputfile, $param_filepath=null) {
if(!array_key_exists('dirname', $param_outputfile)) {
if(!is_null($param_filepath)) {
$path_parts = pathinfo($param_filepath);
$param_outputfile['dirname'] = $path_parts['dirname'].'/';
} else {
$param_outputfile['dirname'] = '/tmp/';
}
}
if(!array_key_exists('prefix', $param_outputfile)) {
if(!is_null($param_filepath)) {
$pathinfo = pathinfo($param_filepath);
$param_outputfile['prefix'] = $pathinfo['filename'];
} else {
$param_outputfile['prefix'] = '';
}
}
if(!array_key_exists('filename', $param_outputfile)) {
$param_outputfile['filename'] = '';
}
if(!array_key_exists('filenameextension', $param_outputfile)) {
$param_outputfile['filenameextension'] = '';
}
}
/**
* @see http://www.selfphp.info/kochbuch/kochbuch.php?code=61
*
* @param type $size
* @param type $praefix
* @param type $short
* @return string
*/
function binary_multiples($size, $praefix=true, $short= true)
{
if($praefix === true)
{
if($short === true)
{
$norm = array('B', 'kB', 'MB', 'GB', 'TB', 'PB',
'EB', 'ZB', 'YB');
}
else
{
$norm = array('Byte',
'Kilobyte',
'Megabyte',
'Gigabyte',
'Terabyte',
'Petabyte',
'Exabyte',
'Zettabyte',
'Yottabyte'
);
}
$factor = 1000;
}
else
{
if($short === true)
{
$norm = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB',
'EiB', 'ZiB', 'YiB');
}
else
{
$norm = array('Byte',
'Kibibyte',
'Mebibyte',
'Gibibyte',
'Tebibyte',
'Pebibyte',
'Exbibyte',
'Zebibyte',
'Yobibyte'
);
}
$factor = 1024;
}
$count = count($norm) -1;
$x = 0;
while ($size >= $factor && $x < $count)
{
$size /= $factor;
$x++;
}
$size = sprintf("%01.2f", $size) . ' ' . $norm[$x];
return $size;
}
}
?>