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
10 
14 
20 class File implements DriverInterface
21 {
25  protected $scheme = '';
26 
32  protected function getWarningMessage()
33  {
34  $warning = error_get_last();
35  if ($warning && $warning['type'] == E_WARNING) {
36  return 'Warning!' . $warning['message'];
37  }
38  return null;
39  }
40 
48  public function isExists($path)
49  {
50  clearstatcache();
51  $result = @file_exists($this->getScheme() . $path);
52  if ($result === null) {
53  throw new FileSystemException(
54  new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
55  );
56  }
57  return $result;
58  }
59 
67  public function stat($path)
68  {
69  clearstatcache();
70  $result = @stat($this->getScheme() . $path);
71  if (!$result) {
72  throw new FileSystemException(
73  new \Magento\Framework\Phrase('Cannot gather stats! %1', [$this->getWarningMessage()])
74  );
75  }
76  return $result;
77  }
78 
86  public function isReadable($path)
87  {
88  clearstatcache();
89  $result = @is_readable($this->getScheme() . $path);
90  if ($result === null) {
91  throw new FileSystemException(
92  new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
93  );
94  }
95  return $result;
96  }
97 
105  public function isFile($path)
106  {
107  clearstatcache();
108  $result = @is_file($this->getScheme() . $path);
109  if ($result === null) {
110  throw new FileSystemException(
111  new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
112  );
113  }
114  return $result;
115  }
116 
124  public function isDirectory($path)
125  {
126  clearstatcache();
127  $result = @is_dir($this->getScheme() . $path);
128  if ($result === null) {
129  throw new FileSystemException(
130  new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
131  );
132  }
133  return $result;
134  }
135 
145  public function fileGetContents($path, $flag = null, $context = null)
146  {
147  clearstatcache();
148  $result = @file_get_contents($this->getScheme() . $path, $flag, $context);
149  if (false === $result) {
150  throw new FileSystemException(
151  new \Magento\Framework\Phrase(
152  'The contents from the "%1" file can\'t be read. %2',
153  [$path, $this->getWarningMessage()]
154  )
155  );
156  }
157  return $result;
158  }
159 
167  public function isWritable($path)
168  {
169  clearstatcache();
170  $result = @is_writable($this->getScheme() . $path);
171  if ($result === null) {
172  throw new FileSystemException(
173  new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
174  );
175  }
176  return $result;
177  }
178 
185  public function getParentDirectory($path)
186  {
187  return dirname($this->getScheme() . $path);
188  }
189 
198  public function createDirectory($path, $permissions = 0777)
199  {
200  return $this->mkdirRecursive($path, $permissions);
201  }
202 
211  private function mkdirRecursive($path, $permissions = 0777)
212  {
213  $path = $this->getScheme() . $path;
214  if (is_dir($path)) {
215  return true;
216  }
217  $parentDir = dirname($path);
218  while (!is_dir($parentDir)) {
219  $this->mkdirRecursive($parentDir, $permissions);
220  }
222  if (!$result) {
223  if (is_dir($path)) {
224  $result = true;
225  } else {
226  throw new FileSystemException(
227  new \Magento\Framework\Phrase(
228  'Directory "%1" cannot be created %2',
229  [$path, $this->getWarningMessage()]
230  )
231  );
232  }
233  }
234  return $result;
235  }
236 
244  public function readDirectory($path)
245  {
246  try {
247  $flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS;
248  $iterator = new \FilesystemIterator($path, $flags);
249  $result = [];
251  foreach ($iterator as $file) {
252  $result[] = $file->getPathname();
253  }
254  sort($result);
255  return $result;
256  } catch (\Exception $e) {
257  throw new FileSystemException(new \Magento\Framework\Phrase($e->getMessage()), $e);
258  }
259  }
260 
269  public function search($pattern, $path)
270  {
271  clearstatcache();
272  $globPattern = rtrim($path, '/') . '/' . ltrim($pattern, '/');
273  $result = Glob::glob($globPattern, Glob::GLOB_BRACE);
274  return is_array($result) ? $result : [];
275  }
276 
286  public function rename($oldPath, $newPath, DriverInterface $targetDriver = null)
287  {
288  $result = false;
289  $targetDriver = $targetDriver ?: $this;
290  if (get_class($targetDriver) == get_class($this)) {
291  $result = @rename($this->getScheme() . $oldPath, $newPath);
292  } else {
293  $content = $this->fileGetContents($oldPath);
294  if (false !== $targetDriver->filePutContents($newPath, $content)) {
295  $result = $this->deleteFile($newPath);
296  }
297  }
298  if (!$result) {
299  throw new FileSystemException(
300  new \Magento\Framework\Phrase(
301  'The path "%1" cannot be renamed into "%2" %3',
302  [$oldPath, $newPath, $this->getWarningMessage()]
303  )
304  );
305  }
306  return $result;
307  }
308 
318  public function copy($source, $destination, DriverInterface $targetDriver = null)
319  {
320  $targetDriver = $targetDriver ?: $this;
321  if (get_class($targetDriver) == get_class($this)) {
322  $result = @copy($this->getScheme() . $source, $destination);
323  } else {
324  $content = $this->fileGetContents($source);
325  $result = $targetDriver->filePutContents($destination, $content);
326  }
327  if (!$result) {
328  throw new FileSystemException(
329  new \Magento\Framework\Phrase(
330  'The file or directory "%1" cannot be copied to "%2" %3',
331  [
332  $source,
333  $destination,
334  $this->getWarningMessage()
335  ]
336  )
337  );
338  }
339  return $result;
340  }
341 
351  public function symlink($source, $destination, DriverInterface $targetDriver = null)
352  {
353  $result = false;
354  if ($targetDriver === null || get_class($targetDriver) == get_class($this)) {
355  $result = @symlink($this->getScheme() . $source, $destination);
356  }
357  if (!$result) {
358  throw new FileSystemException(
359  new \Magento\Framework\Phrase(
360  'A symlink for "%1" can\'t be created and placed to "%2". %3',
361  [
362  $source,
363  $destination,
364  $this->getWarningMessage()
365  ]
366  )
367  );
368  }
369  return $result;
370  }
371 
379  public function deleteFile($path)
380  {
381  $result = @unlink($this->getScheme() . $path);
382  if (!$result) {
383  throw new FileSystemException(
384  new \Magento\Framework\Phrase(
385  'The "%1" file can\'t be deleted. %2',
386  [$path, $this->getWarningMessage()]
387  )
388  );
389  }
390  return $result;
391  }
392 
400  public function deleteDirectory($path)
401  {
402  $flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS;
403  $iterator = new \FilesystemIterator($path, $flags);
405  foreach ($iterator as $entity) {
406  if ($entity->isDir()) {
407  $this->deleteDirectory($entity->getPathname());
408  } else {
409  $this->deleteFile($entity->getPathname());
410  }
411  }
412  $result = @rmdir($this->getScheme() . $path);
413  if (!$result) {
414  throw new FileSystemException(
415  new \Magento\Framework\Phrase(
416  'The directory "%1" cannot be deleted %2',
417  [$path, $this->getWarningMessage()]
418  )
419  );
420  }
421  return $result;
422  }
423 
433  {
434  $result = @chmod($this->getScheme() . $path, $permissions);
435  if (!$result) {
436  throw new FileSystemException(
437  new \Magento\Framework\Phrase(
438  'The permissions can\'t be changed for the "%1" path. %2.',
439  [$path, $this->getWarningMessage()]
440  )
441  );
442  }
443  return $result;
444  }
445 
455  public function changePermissionsRecursively($path, $dirPermissions, $filePermissions)
456  {
457  $result = true;
458  if ($this->isFile($path)) {
459  $result = @chmod($path, $filePermissions);
460  } else {
461  $result = @chmod($path, $dirPermissions);
462  }
463  if (!$result) {
464  throw new FileSystemException(
465  new \Magento\Framework\Phrase(
466  'The permissions can\'t be changed for the "%1" path. %2.',
467  [$path, $this->getWarningMessage()]
468  )
469  );
470  }
471 
472  $flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS;
473 
474  $iterator = new \RecursiveIteratorIterator(
475  new \RecursiveDirectoryIterator($path, $flags),
476  \RecursiveIteratorIterator::CHILD_FIRST
477  );
479  foreach ($iterator as $entity) {
480  if ($entity->isDir()) {
481  $result = @chmod($entity->getPathname(), $dirPermissions);
482  } else {
483  $result = @chmod($entity->getPathname(), $filePermissions);
484  }
485  if (!$result) {
486  throw new FileSystemException(
487  new \Magento\Framework\Phrase(
488  'The permissions can\'t be changed for the "%1" path. %2.',
489  [$path, $this->getWarningMessage()]
490  )
491  );
492  }
493  }
494  return $result;
495  }
496 
505  public function touch($path, $modificationTime = null)
506  {
507  if (!$modificationTime) {
508  $result = @touch($this->getScheme() . $path);
509  } else {
510  $result = @touch($this->getScheme() . $path, $modificationTime);
511  }
512  if (!$result) {
513  throw new FileSystemException(
514  new \Magento\Framework\Phrase(
515  'The "%1" file or directory can\'t be touched. %2',
516  [$path, $this->getWarningMessage()]
517  )
518  );
519  }
520  return $result;
521  }
522 
532  public function filePutContents($path, $content, $mode = null)
533  {
535  if (!$result) {
536  throw new FileSystemException(
537  new \Magento\Framework\Phrase(
538  'The specified "%1" file couldn\'t be written. %2',
539  [$path, $this->getWarningMessage()]
540  )
541  );
542  }
543  return $result;
544  }
545 
554  public function fileOpen($path, $mode)
555  {
556  $result = @fopen($this->getScheme() . $path, $mode);
557  if (!$result) {
558  throw new FileSystemException(
559  new \Magento\Framework\Phrase('File "%1" cannot be opened %2', [$path, $this->getWarningMessage()])
560  );
561  }
562  return $result;
563  }
564 
574  public function fileReadLine($resource, $length, $ending = null)
575  {
576  $result = @stream_get_line($resource, $length, $ending);
577  if (false === $result) {
578  throw new FileSystemException(
579  new \Magento\Framework\Phrase('File cannot be read %1', [$this->getWarningMessage()])
580  );
581  }
582  return $result;
583  }
584 
593  public function fileRead($resource, $length)
594  {
595  $result = @fread($resource, $length);
596  if ($result === false) {
597  throw new FileSystemException(
598  new \Magento\Framework\Phrase('File cannot be read %1', [$this->getWarningMessage()])
599  );
600  }
601  return $result;
602  }
603 
615  public function fileGetCsv($resource, $length = 0, $delimiter = ',', $enclosure = '"', $escape = '\\')
616  {
617  $result = @fgetcsv($resource, $length, $delimiter, $enclosure, $escape);
618  if ($result === null) {
619  throw new FileSystemException(
620  new \Magento\Framework\Phrase(
621  'The "%1" CSV handle is incorrect. Verify the handle and try again.',
622  [$this->getWarningMessage()]
623  )
624  );
625  }
626  return $result;
627  }
628 
636  public function fileTell($resource)
637  {
638  $result = @ftell($resource);
639  if ($result === null) {
640  throw new FileSystemException(
641  new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
642  );
643  }
644  return $result;
645  }
646 
656  public function fileSeek($resource, $offset, $whence = SEEK_SET)
657  {
658  $result = @fseek($resource, $offset, $whence);
659  if ($result === -1) {
660  throw new FileSystemException(
661  new \Magento\Framework\Phrase(
662  'An error occurred during "%1" fileSeek execution.',
663  [$this->getWarningMessage()]
664  )
665  );
666  }
667  return $result;
668  }
669 
676  public function endOfFile($resource)
677  {
678  return feof($resource);
679  }
680 
688  public function fileClose($resource)
689  {
690  $result = @fclose($resource);
691  if (!$result) {
692  throw new FileSystemException(
693  new \Magento\Framework\Phrase(
694  'An error occurred during "%1" fileClose execution.',
695  [$this->getWarningMessage()]
696  )
697  );
698  }
699  return $result;
700  }
701 
710  public function fileWrite($resource, $data)
711  {
712  $lenData = strlen($data);
713  for ($result = 0; $result < $lenData; $result += $fwrite) {
714  $fwrite = @fwrite($resource, substr($data, $result));
715  if (0 === $fwrite) {
716  $this->fileSystemException('Unable to write');
717  }
718  if (false === $fwrite) {
719  $this->fileSystemException(
720  'An error occurred during "%1" fileWrite execution.',
721  [$this->getWarningMessage()]
722  );
723  }
724  }
725 
726  return $result;
727  }
728 
737  private function fileSystemException($message, $arguments = [])
738  {
739  throw new FileSystemException(new \Magento\Framework\Phrase($message, $arguments));
740  }
741 
752  public function filePutCsv($resource, array $data, $delimiter = ',', $enclosure = '"')
753  {
760  foreach ($data as $key => $value) {
761  if (!is_string($value)) {
762  $value = (string)$value;
763  }
764  if (isset($value[0]) && in_array($value[0], ['=', '+', '-'])) {
765  $data[$key] = ' ' . $value;
766  }
767  }
768 
769  $result = @fputcsv($resource, $data, $delimiter, $enclosure);
770  if (!$result) {
771  throw new FileSystemException(
772  new \Magento\Framework\Phrase(
773  'An error occurred during "%1" filePutCsv execution.',
774  [$this->getWarningMessage()]
775  )
776  );
777  }
778  return $result;
779  }
780 
788  public function fileFlush($resource)
789  {
790  $result = @fflush($resource);
791  if (!$result) {
792  throw new FileSystemException(
793  new \Magento\Framework\Phrase(
794  'An error occurred during "%1" fileFlush execution.',
795  [$this->getWarningMessage()]
796  )
797  );
798  }
799  return $result;
800  }
801 
810  public function fileLock($resource, $lockMode = LOCK_EX)
811  {
812  $result = @flock($resource, $lockMode);
813  if (!$result) {
814  throw new FileSystemException(
815  new \Magento\Framework\Phrase(
816  'An error occurred during "%1" fileLock execution.',
817  [$this->getWarningMessage()]
818  )
819  );
820  }
821  return $result;
822  }
823 
831  public function fileUnlock($resource)
832  {
833  $result = @flock($resource, LOCK_UN);
834  if (!$result) {
835  throw new FileSystemException(
836  new \Magento\Framework\Phrase(
837  'An error occurred during "%1" fileUnlock execution.',
838  [$this->getWarningMessage()]
839  )
840  );
841  }
842  return $result;
843  }
844 
851  public function getAbsolutePath($basePath, $path, $scheme = null)
852  {
853  // check if the path given is already an absolute path containing the
854  // basepath. so if the basepath starts at position 0 in the path, we
855  // must not concatinate them again because path is already absolute.
856  if (0 === strpos($path, $basePath)) {
857  return $this->getScheme($scheme) . $path;
858  }
859 
860  return $this->getScheme($scheme) . $basePath . ltrim($this->fixSeparator($path), '/');
861  }
862 
870  public function getRelativePath($basePath, $path = null)
871  {
872  $path = $this->fixSeparator($path);
873  if (strpos($path, $basePath) === 0 || $basePath == $path . '/') {
874  $result = substr($path, strlen($basePath));
875  } else {
876  $result = $path;
877  }
878  return $result;
879  }
880 
888  protected function fixSeparator($path)
889  {
890  return str_replace('\\', '/', $path);
891  }
892 
899  protected function getScheme($scheme = null)
900  {
901  return $scheme ? $scheme . '://' : '';
902  }
903 
911  public function readDirectoryRecursively($path = null)
912  {
913  $result = [];
914  $flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS;
915  try {
916  $iterator = new \RecursiveIteratorIterator(
917  new \RecursiveDirectoryIterator($path, $flags),
918  \RecursiveIteratorIterator::CHILD_FIRST
919  );
921  foreach ($iterator as $file) {
922  $result[] = $file->getPathname();
923  }
924  } catch (\Exception $e) {
925  throw new FileSystemException(new \Magento\Framework\Phrase($e->getMessage()), $e);
926  }
927  return $result;
928  }
929 
937  public function getRealPath($path)
938  {
939  return realpath($path);
940  }
941 
948  public function getRealPathSafety($path)
949  {
950  if (strpos($path, DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR) === false) {
951  return $path;
952  }
953 
954  //Removing redundant directory separators.
955  $path = preg_replace(
956  '/\\' .DIRECTORY_SEPARATOR .'\\' .DIRECTORY_SEPARATOR .'+/',
957  DIRECTORY_SEPARATOR,
958  $path
959  );
960  $pathParts = explode(DIRECTORY_SEPARATOR, $path);
961  $realPath = [];
962  foreach ($pathParts as $pathPart) {
963  if ($pathPart == '.') {
964  continue;
965  }
966  if ($pathPart == '..') {
967  array_pop($realPath);
968  continue;
969  }
970  $realPath[] = $pathPart;
971  }
972  return implode(DIRECTORY_SEPARATOR, $realPath);
973  }
974 }
createDirectory($path, $permissions=0777)
Definition: File.php:198
copy($source, $destination, DriverInterface $targetDriver=null)
Definition: File.php:318
symlink($source, $destination, DriverInterface $targetDriver=null)
Definition: File.php:351
getAbsolutePath($basePath, $path, $scheme=null)
Definition: File.php:851
$pattern
Definition: website.php:22
$source
Definition: source.php:23
static glob($pattern, $flags=0, $forceFallback=false)
Definition: Glob.php:24
$resource
Definition: bulk.php:12
$message
fileGetContents($path, $flag=null, $context=null)
Definition: File.php:145
filePutContents($path, $content, $mode=null)
Definition: File.php:532
fileGetCsv($resource, $length=0, $delimiter=',', $enclosure='"', $escape = '\\')
Definition: File.php:615
$value
Definition: gender.phtml:16
rename($oldPath, $newPath, DriverInterface $targetDriver=null)
Definition: File.php:286
fileReadLine($resource, $length, $ending=null)
Definition: File.php:574
touch($path, $modificationTime=null)
Definition: File.php:505
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
fileSeek($resource, $offset, $whence=SEEK_SET)
Definition: File.php:656
$entity
Definition: element.phtml:22
is_writable($path)
Definition: io.php:25
$arguments
changePermissions($path, $permissions)
Definition: File.php:432
changePermissionsRecursively($path, $dirPermissions, $filePermissions)
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25
getRelativePath($basePath, $path=null)
Definition: File.php:870
$permissions
fileLock($resource, $lockMode=LOCK_EX)
Definition: File.php:810
filePutCsv($resource, array $data, $delimiter=',', $enclosure='"')