Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileHelper.php
Go to the documentation of this file.
1 <?php
7 
12 {
20  public function normalizePath($path, $ds = DIRECTORY_SEPARATOR)
21  {
22  $path = rtrim(strtr($path, '/\\', $ds . $ds), $ds);
23  if (strpos($ds . $path, "{$ds}.") === false && strpos($path, "{$ds}{$ds}") === false) {
24  return $path;
25  }
26 
27  return $this->realpath($ds, $path);
28  }
29 
37  private function realpath($ds, $path)
38  {
39  $parts = [];
40  foreach (explode($ds, $path) as $part) {
41  if ($part === '..' && !empty($parts) && end($parts) !== '..') {
42  array_pop($parts);
43  } elseif ($part === '.' || $part === '' && !empty($parts)) {
44  continue;
45  } else {
46  $parts[] = $part;
47  }
48  }
49 
50  $path = implode($ds, $parts);
51 
52  return $path === '' ? '.' : $path;
53  }
54 
64  public function createDirectory($path, $mode = 0775, $recursive = true)
65  {
66  if (is_dir($path)) {
67  return true;
68  }
69  $parentDir = dirname($path);
70 
71  if ($recursive && !is_dir($parentDir) && $parentDir !== $path) {
72  $this->createDirectory($parentDir, $mode, true);
73  }
74 
75  try {
76  if (!mkdir($path, $mode)) {
77  return false;
78  }
79  } catch (\Exception $e) {
80  if (!is_dir($path)) {
81  throw new \Exception("Failed to create directory \"$path\"");
82  }
83  }
84 
85  try {
86  return chmod($path, $mode);
87  } catch (\Exception $e) {
88  throw new \Exception("Failed to change permissions for directory \"$path\"");
89  }
90  }
91 
99  public function createFile($filename, $content)
100  {
101  return file_put_contents($filename, $content) !== false;
102  }
103 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
normalizePath($path, $ds=DIRECTORY_SEPARATOR)
Definition: FileHelper.php:20
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25
createDirectory($path, $mode=0775, $recursive=true)
Definition: FileHelper.php:64