Bitrix-D7  20.0.0
main/lib/io/directory.php
См. документацию.
1 <?php
2 namespace Bitrix\Main\IO;
3 
4 class Directory
5  extends DirectoryEntry
6 {
7  public function __construct($path, $siteId = null)
8  {
10  }
11 
12  public function isExists()
13  {
14  $p = $this->getPhysicalPath();
15  return file_exists($p) && is_dir($p);
16  }
17 
18  public function delete()
19  {
20  return self::deleteInternal($this->getPhysicalPath());
21  }
22 
23  private static function deleteInternal($path)
24  {
25  if (is_file($path) || is_link($path))
26  {
27  if (!@unlink($path))
28  throw new FileDeleteException($path);
29  }
30  elseif (is_dir($path))
31  {
32  if ($handle = opendir($path))
33  {
34  while (($file = readdir($handle)) !== false)
35  {
36  if ($file == "." || $file == "..")
37  continue;
38 
39  self::deleteInternal(Path::combine($path, $file));
40  }
41  closedir($handle);
42  }
43  if (!@rmdir($path))
44  throw new FileDeleteException($path);
45  }
46 
47  return true;
48  }
49 
50  /**
51  * @return array|FileSystemEntry[]
52  * @throws FileNotFoundException
53  */
54  public function getChildren()
55  {
56  if (!$this->isExists())
57  throw new FileNotFoundException($this->originalPath);
58 
59  $arResult = array();
60 
61  if ($handle = opendir($this->getPhysicalPath()))
62  {
63  while (($file = readdir($handle)) !== false)
64  {
65  if ($file == "." || $file == "..")
66  continue;
67 
68  $pathLogical = Path::combine($this->path, Path::convertPhysicalToLogical($file));
69  $pathPhysical = Path::combine($this->getPhysicalPath(), $file);
70  if (is_dir($pathPhysical))
71  $arResult[] = new Directory($pathLogical, $this->siteId);
72  else
73  $arResult[] = new File($pathLogical, $this->siteId);
74  }
75  closedir($handle);
76  }
77 
78  return $arResult;
79  }
80 
81  /**
82  * @param $name
83  * @return Directory|DirectoryEntry
84  */
85  public function createSubdirectory($name)
86  {
87  $dir = new Directory(Path::combine($this->path, $name));
88  if (!$dir->isExists())
89  mkdir($dir->getPhysicalPath(), BX_DIR_PERMISSIONS, true);
90  return $dir;
91  }
92 
93  public function getCreationTime()
94  {
95  if (!$this->isExists())
96  throw new FileNotFoundException($this->originalPath);
97 
98  return filectime($this->getPhysicalPath());
99  }
100 
101  public function getLastAccessTime()
102  {
103  if (!$this->isExists())
104  throw new FileNotFoundException($this->originalPath);
105 
106  return fileatime($this->getPhysicalPath());
107  }
108 
109  public function getModificationTime()
110  {
111  if (!$this->isExists())
112  throw new FileNotFoundException($this->originalPath);
113 
114  return filemtime($this->getPhysicalPath());
115  }
116 
117  public function markWritable()
118  {
119  if (!$this->isExists())
120  throw new FileNotFoundException($this->originalPath);
121 
122  @chmod($this->getPhysicalPath(), BX_DIR_PERMISSIONS);
123  }
124 
125  public function getPermissions()
126  {
127  return fileperms($this->getPhysicalPath());
128  }
129 
130  /**
131  * @param $path
132  *
133  * @return Directory
134  */
135  public static function createDirectory($path)
136  {
137  $dir = new self($path);
138  $dir->create();
139 
140  return $dir;
141  }
142 
143  public static function deleteDirectory($path)
144  {
145  $dir = new self($path);
146  $dir->delete();
147  }
148 
149  public static function isDirectoryExists($path)
150  {
151  $f = new self($path);
152  return $f->isExists();
153  }
154 }
Bitrix\Main\IO\Directory\__construct
__construct($path, $siteId=null)
Definition: main/lib/io/directory.php:7
Bitrix\Main\IO\FileSystemEntry\$path
$path
Definition: filesystementry.php:8
Bitrix\Main\IO\FileSystemEntry\$pathPhysical
$pathPhysical
Definition: filesystementry.php:10
Bitrix\Main\IO\FileNotFoundException
Definition: ioexception.php:44
Bitrix\Main\IO\FileSystemEntry\getPhysicalPath
getPhysicalPath()
Definition: filesystementry.php:90
Bitrix\Main\IO\Directory\isDirectoryExists
static isDirectoryExists($path)
Definition: main/lib/io/directory.php:149
Bitrix\Main\IO\FileSystemEntry\$siteId
$siteId
Definition: filesystementry.php:11
Bitrix\Main\IO\Path\combine
static combine()
Definition: main/lib/io/path.php:221
Bitrix\Main\IO\Directory\getLastAccessTime
getLastAccessTime()
Definition: main/lib/io/directory.php:101
Bitrix\Main\IO\Directory\createDirectory
static createDirectory($path)
Definition: main/lib/io/directory.php:135
Bitrix\Main\IO\Directory
Definition: main/lib/io/directory.php:4
Bitrix\Main\IO\Directory\isExists
isExists()
Definition: main/lib/io/directory.php:12
Bitrix\Main\IO\Directory\getPermissions
getPermissions()
Definition: main/lib/io/directory.php:125
Bitrix\Main\IO\Directory\getCreationTime
getCreationTime()
Definition: main/lib/io/directory.php:93
Bitrix\Main\IO\Directory\markWritable
markWritable()
Definition: main/lib/io/directory.php:117
Bitrix\Sender\Connector\__construct
__construct(Base $connector)
Constructor.
Definition: resultview.php:40
Bitrix\Main\IO\DirectoryEntry
Definition: directoryentry.php:4
Bitrix\Main\IO\Path\convertPhysicalToLogical
static convertPhysicalToLogical($path)
Definition: main/lib/io/path.php:128
Bitrix\Main\IO
Definition: main/lib/io/directory.php:2
Bitrix\Main\IO\FileDeleteException
Definition: ioexception.php:53
Bitrix\Main\IO\Directory\getChildren
getChildren()
Definition: main/lib/io/directory.php:54
Bitrix\Main\IO\File
Definition: main/lib/io/file.php:4
Bitrix\Main\IO\Directory\getModificationTime
getModificationTime()
Definition: main/lib/io/directory.php:109
Bitrix\Main\IO\Directory\deleteDirectory
static deleteDirectory($path)
Definition: main/lib/io/directory.php:143
Bitrix\Main\IO\Directory\createSubdirectory
createSubdirectory($name)
Definition: main/lib/io/directory.php:85