Bitrix-D7  20.0.0
main/lib/io/file.php
См. документацию.
1 <?php
2 namespace Bitrix\Main\IO;
3 
4 class File
5  extends FileEntry
6  implements IFileStream
7 {
8  const REWRITE = 0;
9  const APPEND = 1;
10 
11  /** @var resource */
12  protected $filePointer;
13 
14  public function __construct($path, $siteId = null)
15  {
17  }
18 
19  /**
20  * Opens the file and returns the file pointer.
21  *
22  * @param string $mode
23  * @return resource
24  * @throws FileOpenException
25  */
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 
36  /**
37  * Closes the file.
38  *
39  * @throws FileNotOpenedException
40  */
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 
79  /**
80  * Returns the file size.
81  *
82  * @return float|int
83  * @throws FileNotFoundException
84  * @throws FileOpenException
85  */
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 
133  /**
134  * Seeks on the file pointer from the beginning (SEEK_SET only).
135  *
136  * @param int|float $position
137  * @return int
138  * @throws FileNotOpenedException
139  */
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 }
Bitrix\Main\IO\FileSystemEntry\$path
$path
Definition: filesystementry.php:8
Bitrix\Main\IO\File\open
open($mode)
Opens the file and returns the file pointer.
Definition: main/lib/io/file.php:26
Bitrix\Main\IO\File\close
close()
Closes the file.
Definition: main/lib/io/file.php:41
Bitrix\Main\IO\File\seek
seek($position)
Seeks on the file pointer from the beginning (SEEK_SET only).
Definition: main/lib/io/file.php:140
Bitrix\Main\IO\File\isExists
isExists()
Definition: main/lib/io/file.php:51
Bitrix\Main\IO\File\isWritable
isWritable()
Definition: main/lib/io/file.php:172
Bitrix\Main\IO\FileNotFoundException
Definition: ioexception.php:44
Bitrix\Main\IO\FileSystemEntry\getPhysicalPath
getPhysicalPath()
Definition: filesystementry.php:90
Bitrix\Main\IO\FileStreamOpenMode\READ
const READ
Definition: ifilestream.php:11
Bitrix\Main\IO\FileNotOpenedException
Definition: ioexception.php:71
Bitrix\Main\IO\FileSystemEntry\$siteId
$siteId
Definition: filesystementry.php:11
Bitrix\Main\IO\File\getContentType
getContentType()
Definition: main/lib/io/file.php:244
Bitrix\Main\IO\File\getPermissions
getPermissions()
Definition: main/lib/io/file.php:228
Bitrix\Main\IO\File\isReadable
isReadable()
Definition: main/lib/io/file.php:180
Bitrix\Main\IO\File\$filePointer
$filePointer
Definition: main/lib/io/file.php:12
Bitrix\Main\IO\File\isFileExists
static isFileExists($path)
Definition: main/lib/io/file.php:256
Bitrix\Main\IO\File\REWRITE
const REWRITE
Definition: main/lib/io/file.php:8
Bitrix\Main\IO\File\getContents
getContents()
Definition: main/lib/io/file.php:57
Bitrix\Main\IO\File\getCreationTime
getCreationTime()
Definition: main/lib/io/file.php:196
Bitrix\Main\IO\File\putFileContents
static putFileContents($path, $data, $flags=self::REWRITE)
Definition: main/lib/io/file.php:268
Bitrix\Main\IO\File\markWritable
markWritable()
Definition: main/lib/io/file.php:220
Bitrix\Main\IO\File\getSize
getSize()
Returns the file size.
Definition: main/lib/io/file.php:86
Bitrix\Main\IO\File\__construct
__construct($path, $siteId=null)
Definition: main/lib/io/file.php:14
Bitrix\Sender\Connector\__construct
__construct(Base $connector)
Constructor.
Definition: resultview.php:40
Bitrix\Main\IO\File\deleteFile
static deleteFile($path)
Definition: main/lib/io/file.php:274
Bitrix\Main\IO\File\getLastAccessTime
getLastAccessTime()
Definition: main/lib/io/file.php:204
Bitrix\Main\IO
Definition: main/lib/io/directory.php:2
Bitrix\Main\IO\FileSystemEntry\getDirectory
getDirectory()
Definition: filesystementry.php:71
Bitrix\Main\IO\File\putContents
putContents($data, $flags=self::REWRITE)
Definition: main/lib/io/file.php:65
Bitrix\Main\IO\File\getFileContents
static getFileContents($path)
Definition: main/lib/io/file.php:262
Bitrix\Main\IO\File
Definition: main/lib/io/file.php:4
Bitrix\Main\IO\FileOpenException
Definition: ioexception.php:62
Bitrix\Main\IO\IFileStream
Definition: ifilestream.php:4
Bitrix\Main\IO\FileSystemEntry\getPath
getPath()
Definition: filesystementry.php:66
Bitrix\Main\IO\FileEntry
Definition: fileentry.php:4
Bitrix\Main\IO\File\getModificationTime
getModificationTime()
Definition: main/lib/io/file.php:212
Bitrix\Main\IO\File\APPEND
const APPEND
Definition: main/lib/io/file.php:9
Bitrix\Main\IO\File\readFile
readFile()
Definition: main/lib/io/file.php:188