Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
File.php
Go to the documentation of this file.
1 <?php
11 
13 
14 class File
15 {
21  protected $_fileLocation;
22 
28  protected $_fileName;
29 
35  protected $_filePath;
36 
42  protected $_chmod;
43 
49  protected $_fileHandler;
50 
56  protected $_isInWriteMode;
57 
63  public function __construct($filePath)
64  {
65  $pathInfo = pathinfo($filePath);
66 
67  $this->_filePath = $filePath;
68  $this->_fileLocation = isset($pathInfo['dirname']) ? $pathInfo['dirname'] : '';
69  $this->_fileName = isset($pathInfo['basename']) ? $pathInfo['basename'] : '';
70  }
71 
75  public function __destruct()
76  {
77  if ($this->_fileHandler) {
78  $this->_close();
79  }
80  }
81 
91  public function open($mode = 'w+', $chmod = null)
92  {
93  $this->_isInWriteMode = $this->_isWritableMode($mode);
94 
95  if ($this->_isInWriteMode) {
96  if (!is_writable($this->_fileLocation)) {
97  throw new LocalizedException(
98  new \Magento\Framework\Phrase(
99  'You don\'t have permissions to write to the "%1" file.',
100  [$this->_fileLocation]
101  )
102  );
103  }
104 
105  if (is_file($this->_filePath) && !is_writable($this->_filePath)) {
106  throw new LocalizedException(
107  new \Magento\Framework\Phrase(
108  'You don\'t have the permissions to open the "%1" file for writing access.',
109  [$this->_fileName]
110  )
111  );
112  }
113  }
114 
115  if ($this->_isReadableMode($mode) && (!is_file($this->_filePath) || !is_readable($this->_filePath))) {
116  if (!is_file($this->_filePath)) {
117  throw new LocalizedException(
118  new \Magento\Framework\Phrase(
119  'The "%1" file doesn\'t exist. Verify the file and try again.',
120  [$this->_filePath]
121  )
122  );
123  }
124 
125  if (!is_readable($this->_filePath)) {
126  throw new LocalizedException(
127  new \Magento\Framework\Phrase(
128  'You don\'t have permissions to read the "%1" file.',
129  [$this->_filePath]
130  )
131  );
132  }
133  }
134 
135  $this->_open($mode);
136 
137  $this->_chmod = $chmod;
138  }
139 
146  public function write($data)
147  {
148  $this->_checkFileOpened();
149  $this->_write($data);
150  }
151 
158  public function read($length = 4096)
159  {
160  $data = false;
161  $this->_checkFileOpened();
162  if ($length > 0) {
163  $data = $this->_read($length);
164  }
165 
166  return $data;
167  }
168 
174  public function eof()
175  {
176  $this->_checkFileOpened();
177  return $this->_eof();
178  }
179 
185  public function close()
186  {
187  $this->_checkFileOpened();
188  $this->_close();
189  $this->_fileHandler = false;
190 
191  if ($this->_isInWriteMode && isset($this->_chmod)) {
192  @chmod($this->_filePath, $this->_chmod);
193  }
194  }
195 
203  protected function _open($mode)
204  {
205  $this->_fileHandler = @fopen($this->_filePath, $mode);
206 
207  if (false === $this->_fileHandler) {
208  throw new LocalizedException(
209  new \Magento\Framework\Phrase('The "%1" file failed to open.', [$this->_filePath])
210  );
211  }
212  }
213 
221  protected function _write($data)
222  {
223  $result = @fwrite($this->_fileHandler, $data);
224 
225  if (false === $result) {
226  throw new LocalizedException(
227  new \Magento\Framework\Phrase('The data failed to write to "%1".', [$this->_filePath])
228  );
229  }
230  }
231 
239  protected function _read($length)
240  {
241  $result = fread($this->_fileHandler, $length);
242 
243  if (false === $result) {
244  throw new LocalizedException(
245  new \Magento\Framework\Phrase('Failed to read data from %1', [$this->_filePath])
246  );
247  }
248 
249  return $result;
250  }
251 
257  protected function _eof()
258  {
259  return feof($this->_fileHandler);
260  }
261 
267  protected function _close()
268  {
269  fclose($this->_fileHandler);
270  }
271 
278  protected function _isWritableMode($mode)
279  {
280  return preg_match('/(^[waxc])|(\+$)/', $mode);
281  }
282 
289  protected function _isReadableMode($mode)
290  {
291  return !$this->_isWritableMode($mode);
292  }
293 
300  protected function _checkFileOpened()
301  {
302  if (!$this->_fileHandler) {
303  throw new LocalizedException(new \Magento\Framework\Phrase('File not opened'));
304  }
305  }
306 }
open($mode='w+', $chmod=null)
Definition: File.php:91
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
is_writable($path)
Definition: io.php:25