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