class_mcFile.inc.php
4.43 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
<?php
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) {
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);
// D::show($pathinfo, 'basename: '.basename($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'] = '';
}
D::show($param_outputfile, '$param_outputfile');
}
}
?>