File io/filesystem/Folder.class.php
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217:
<?php
/**
* This class allows you to handle very easily a folder on the server.
* @package IO
* @subpackage Filesystem
* @copyright © 2005-2019 PHPBoost
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
* @author Nicolas Duhamel <akhenathon2@gmail.com>
* @version PHPBoost 5.2 - last update: 2016 10 24
* @since PHPBoost 2.0 - 2008 07 06
* @contributor Loic ROUCHON <horn@phpboost.com>
* @contributor Benoit SAUTEL <ben.popeye@phpboost.com>
* @contributor Arnaud GENET <elenwii@phpboost.com>
*/
class Folder extends FileSystemElement
{
private $opened = false;
/**
* @var File[] List of the files contained by this folder.
*/
private $files = array();
/**
* @var Folder[] List of the folders contained by this folder.
*/
private $folders = array();
/**
* Builds a Folder object.
* @param string $path Path of the folder.
*/
public function __construct($path)
{
parent::__construct(rtrim($path, '/'));
}
/**
* Returns true if the folder exists after this call, else, false
* @return bool true if the folder exists after this call, else, false
*/
public function create()
{
$path = $this->get_path();
if (@file_exists($path))
{
if (!@is_dir($path))
{
return false;
}
}
else if (!@mkdir($path))
{
return false;
}
return true;
}
/**
* Opens the folder.
*/
private function open()
{
if (!$this->opened)
{
$this->files = array();
$this->folders = array();
$path = $this->get_path();
if ($dh = @opendir($path))
{
while (!is_bool($fse_name = readdir($dh)))
{
if ($fse_name == '.' || $fse_name == '..')
{
continue;
}
$file = $path . '/' . $fse_name;
if (!is_link($file))
{
if (is_dir($file))
{
$this->folders[] = new Folder($file);
}
else
{
$this->files[] = new File($file);
}
}
}
closedir($dh);
}
else
{
throw new IOException('Can\'t open folder : ' . $this->get_path());
}
$this->opened = true;
}
}
/**
* Lists the files contained in this folder.
* @param string $regex PREG which describes the pattern the files you want to list must match. If you want all of them, don't use this parameter.
* @param bool $regex_exclude_files true if the regex to exclude files
* @return File[] The files list.
*/
public function get_files($regex = '', $regex_exclude_files = false)
{
$this->open();
if (empty($regex))
{
return $this->files;
}
$files = array();
foreach ($this->files as $file)
{
if ($regex_exclude_files)
{
if (!preg_match($regex, $file->get_name()))
{
$files[] = $file;
}
}
else
{
if (preg_match($regex, $file->get_name()))
{
$files[] = $file;
}
}
}
return $files;
}
/**
* Lists the folders contained in this folder.
* @param string $regex PREG which describes the pattern the folders you want to list must match. If you want all of them, don't use this parameter.
* @return Folder[] The folders list.
*/
public function get_folders($regex = '')
{
$this->open();
if (empty($regex))
{
return $this->folders;
}
$folders = array();
foreach ($this->folders as $folder)
{
if (preg_match($regex, $folder->get_name()))
{
$folders[] = $folder;
}
}
return $folders;
}
/**
* Returns the first folder present in this folder
* @return Folder The first folder of this folder or null if it doesn't contain any folder.
*/
public function get_first_folder()
{
$this->open();
if (isset($this->folders[0]))
{
return $this->folders[0];
}
else
{
return null;
}
}
/**
* Returns all the file system elements contained by the folder.
* @return FileSystemElement[] The list of the file system element contained in this folder.
*/
public function get_all_content()
{
return array_merge($this->get_files(), $this->get_folders());
}
/**
* Deletes the folder and all what it contains.
* @return True if deleted successfully.
*/
public function delete()
{
$fs = $this->get_all_content();
foreach ($fs as $fse)
{
$fse->delete();
}
if (!@rmdir($this->get_path()) && !file_exists($this->get_path()))
{
throw new IOException('The folder ' . $this->get_path() . ' couldn\'t been deleted');
}
}
/**
* Returns the date of the last modification of the folder.
* @return int The UNIX timestamp corresponding to the last modification date.
*/
public function get_last_modification_date()
{
$folder_infos = stat($this->get_path());
return $folder_infos['mtime'];
}
}
?>