Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
file.php
1<?php
2namespace Bitrix\Main\IO;
3
4class File
5 extends FileEntry
6 implements IFileStream
7{
8 const REWRITE = 0;
9 const APPEND = 1;
10
12 protected $filePointer;
13
14 public function __construct($path, $siteId = null)
15 {
16 parent::__construct($path, $siteId);
17 }
18
26 public function open($mode)
27 {
28 $this->filePointer = fopen($this->getPhysicalPath(), $mode."b");
29 if (!$this->filePointer)
30 {
31 throw new FileOpenException($this->originalPath);
32 }
33 return $this->filePointer;
34 }
35
41 public function close()
42 {
43 if(!$this->filePointer)
44 {
45 throw new FileNotOpenedException($this->originalPath);
46 }
47 fclose($this->filePointer);
48 $this->filePointer = null;
49 }
50
51 public function isExists()
52 {
53 $p = $this->getPhysicalPath();
54 return file_exists($p) && (is_file($p) || is_link($p));
55 }
56
57 public function getContents()
58 {
59 if (!$this->isExists())
60 throw new FileNotFoundException($this->originalPath);
61
62 return file_get_contents($this->getPhysicalPath());
63 }
64
65 public function putContents($data, $flags = self::REWRITE)
66 {
67 $dir = $this->getDirectory();
68 if (!$dir->isExists())
69 $dir->create();
70
71 if ($this->isExists() && !$this->isWritable())
72 $this->markWritable();
73
74 return $flags & self::APPEND
75 ? file_put_contents($this->getPhysicalPath(), $data, FILE_APPEND)
76 : file_put_contents($this->getPhysicalPath(), $data);
77 }
78
86 public function getSize()
87 {
88 if (!$this->isExists())
89 {
90 throw new FileNotFoundException($this->originalPath);
91 }
92
93 static $supportLarge32 = null;
94 if($supportLarge32 === null)
95 {
96 $supportLarge32 = (\Bitrix\Main\Config\Configuration::getValue("large_files_32bit_support") === true);
97 }
98
99 $size = 0;
100 if(PHP_INT_SIZE < 8 && $supportLarge32)
101 {
102 // 32bit
104
105 if(fseek($this->filePointer, 0, SEEK_END) === 0)
106 {
107 $size = 0.0;
108 $step = 0x7FFFFFFF;
109 while($step > 0)
110 {
111 if (fseek($this->filePointer, -$step, SEEK_CUR) === 0)
112 {
113 $size += floatval($step);
114 }
115 else
116 {
117 $step >>= 1;
118 }
119 }
120 }
121
122 $this->close();
123 }
124 else
125 {
126 // 64bit
127 $size = filesize($this->getPhysicalPath());
128 }
129
130 return $size;
131 }
132
140 public function seek($position)
141 {
142 if(!$this->filePointer)
143 {
144 throw new FileNotOpenedException($this->originalPath);
145 }
146
147 if($position <= PHP_INT_MAX)
148 {
149 return fseek($this->filePointer, $position, SEEK_SET);
150 }
151 else
152 {
153 $res = fseek($this->filePointer, 0, SEEK_SET);
154 if($res === 0)
155 {
156 do
157 {
158 $offset = ($position < PHP_INT_MAX? $position : PHP_INT_MAX);
159 $res = fseek($this->filePointer, $offset, SEEK_CUR);
160 if($res !== 0)
161 {
162 break;
163 }
164 $position -= PHP_INT_MAX;
165 }
166 while($position > 0);
167 }
168 return $res;
169 }
170 }
171
172 public function isWritable()
173 {
174 if (!$this->isExists())
175 throw new FileNotFoundException($this->originalPath);
176
177 return is_writable($this->getPhysicalPath());
178 }
179
180 public function isReadable()
181 {
182 if (!$this->isExists())
183 throw new FileNotFoundException($this->originalPath);
184
185 return is_readable($this->getPhysicalPath());
186 }
187
188 public function readFile()
189 {
190 if (!$this->isExists())
191 throw new FileNotFoundException($this->originalPath);
192
193 return readfile($this->getPhysicalPath());
194 }
195
196 public function getCreationTime()
197 {
198 if (!$this->isExists())
199 throw new FileNotFoundException($this->originalPath);
200
201 return filectime($this->getPhysicalPath());
202 }
203
204 public function getLastAccessTime()
205 {
206 if (!$this->isExists())
207 throw new FileNotFoundException($this->originalPath);
208
209 return fileatime($this->getPhysicalPath());
210 }
211
212 public function getModificationTime()
213 {
214 if (!$this->isExists())
215 throw new FileNotFoundException($this->originalPath);
216
217 return filemtime($this->getPhysicalPath());
218 }
219
220 public function markWritable()
221 {
222 if (!$this->isExists())
223 throw new FileNotFoundException($this->originalPath);
224
225 @chmod($this->getPhysicalPath(), BX_FILE_PERMISSIONS);
226 }
227
228 public function getPermissions()
229 {
230 if (!$this->isExists())
231 throw new FileNotFoundException($this->originalPath);
232
233 return fileperms($this->getPhysicalPath());
234 }
235
236 public function delete()
237 {
238 if ($this->isExists())
239 return unlink($this->getPhysicalPath());
240
241 return true;
242 }
243
244 public function getContentType()
245 {
246 if (!$this->isExists())
247 throw new FileNotFoundException($this->originalPath);
248
249 $finfo = \finfo_open(FILEINFO_MIME_TYPE);
250 $contentType = \finfo_file($finfo, $this->getPath());
251 \finfo_close($finfo);
252
253 return $contentType;
254 }
255
256 public static function isFileExists($path)
257 {
258 $f = new self($path);
259 return $f->isExists();
260 }
261
262 public static function getFileContents($path)
263 {
264 $f = new self($path);
265 return $f->getContents();
266 }
267
268 public static function putFileContents($path, $data, $flags=self::REWRITE)
269 {
270 $f = new self($path);
271 return $f->putContents($data, $flags);
272 }
273
274 public static function deleteFile($path)
275 {
276 $f = new self($path);
277 return $f->delete();
278 }
279}
Definition fileentry.php:6
static deleteFile($path)
Definition file.php:274
static isFileExists($path)
Definition file.php:256
seek($position)
Definition file.php:140
static putFileContents($path, $data, $flags=self::REWRITE)
Definition file.php:268
putContents($data, $flags=self::REWRITE)
Definition file.php:65
static getFileContents($path)
Definition file.php:262
__construct($path, $siteId=null)
Definition file.php:14
$path
getPath()
$siteId
getPhysicalPath()
getDirectory()