1: <?php
2:
3: namespace webfilesframework\core\datasystem\file\system;
4:
5: use webfilesframework\core\datasystem\file\format\MWebfile;
6:
7: 8: 9: 10: 11: 12:
13: class MFile extends MWebfile
14: {
15:
16: protected $m_sFolderName = "./";
17: protected $m_sFileName;
18:
19: protected $m_dDate;
20:
21:
22: public function __construct($fileName)
23: {
24:
25:
26: if ($this->containsFileseperator($fileName)) {
27: $this->m_sFolderName = static::extractFolderName($fileName);
28: $this->m_sFileName = static::extractFileName($fileName);
29: } else {
30: $this->m_sFileName = $fileName;
31: }
32:
33:
34: if ($this->exists()) {
35: $this->setDate(filemtime($this->getPath()));
36: }
37: }
38:
39:
40: public function containsFileseperator($fileName)
41: {
42: if (strpos($fileName, "\\") !== FALSE) {
43: return true;
44: } else if (strpos($fileName, "/") !== FALSE) {
45: return true;
46: }
47: return false;
48: }
49:
50: 51: 52: 53:
54: public function getContent()
55: {
56: return file_get_contents($this->getPath());
57: }
58:
59: 60: 61: 62: 63: 64:
65: public function writeContent($content, $overwrite = false)
66: {
67:
68: if ($overwrite) {
69: $fh = fopen($this->getPath(), 'w');
70: } else {
71: $fh = fopen($this->getPath(), 'w+');
72: }
73: fwrite($fh, $content);
74: fclose($fh);
75: }
76:
77: 78: 79:
80: public function exists()
81: {
82: return (file_exists($this->getPath()) || @fopen($this->getPath(), "r") == true);
83: }
84:
85: 86: 87: 88:
89: public static function extractFileName($filePath)
90: {
91: if (strrpos($filePath, "/") !== FALSE) {
92: $filePath = preg_replace('~(/+)~', '/', $filePath);
93: return substr($filePath, strrpos($filePath, "/") + 1);
94: } else if (strrpos($filePath, "\\") !== FALSE) {
95: return substr($filePath, strrpos($filePath, "\\") + 1);
96: } else {
97: return $filePath;
98: }
99: }
100:
101: 102: 103: 104:
105: public static function extractFolderName($filePath)
106: {
107: if (strrpos($filePath, "/") !== FALSE) {
108: $filePath = preg_replace('~(/+)~', '/', $filePath);
109: return substr($filePath, 0, strrpos($filePath, "/") + 1);
110: } else if (strrpos($filePath, "\\") !== FALSE) {
111: return substr($filePath, 0, strrpos($filePath, "\\") + 1);
112: } else {
113: return $filePath;
114: }
115: }
116:
117: public function renameTo($newFileName) {
118: $oldPath = $this->getPath();
119: $newPath = $this->m_sFolderName . "/" . $newFileName;
120:
121: rename($oldPath,$newPath);
122:
123: $this->m_sFileName = $newFileName;
124: }
125:
126: 127: 128:
129: public function delete()
130: {
131: unlink($this->getPath());
132: }
133:
134: 135: 136:
137: public function getPath()
138: {
139: return $this->m_sFolderName . "/" . $this->m_sFileName;
140: }
141:
142: 143: 144:
145: public function getName()
146: {
147: return $this->m_sFileName;
148: }
149:
150: 151: 152:
153: public function getFolder()
154: {
155: return $this->m_sFolderName;
156: }
157:
158: 159: 160:
161: public function getExtension()
162: {
163:
164: $pointPosition = strrpos($this->m_sFileName, ".");
165:
166: if ($pointPosition === FALSE) {
167: return "";
168: } else {
169: return substr($this->m_sFileName, $pointPosition + 1);
170: }
171:
172: }
173:
174: public function getDate()
175: {
176: return $this->m_dDate;
177: }
178:
179: public function setDate($date)
180: {
181: $this->m_dDate = $date;
182: }
183:
184: public function __toString()
185: {
186: return "File \"" . $this->getName() . "\"<br />";
187: }
188:
189: }