protected function addFile(string $filename, string $dir = '') : int
{
if (!empty($dir)) {
if ($dir[\strlen($dir) - 1] !== DIRECTORY_SEPARATOR) {
$dir .= DIRECTORY_SEPARATOR;
}
}
if (!\file_exists($filename)) {
echo $this->c['red'], 'File not found: ', $this->c[''], $filename, "\n";
return 0;
}
try {
$path = $this->getRealPath(\realpath($dir . $filename));
} catch (\Error $e) {
echo $this->c['red'], $e->getMessage(), $this->c[''], "\n";
return 0;
}
if (\in_array($path, $this->session['add'])) {
echo $this->c['yellow'], 'File already added: ', $this->c[''], $path, "\n";
return 0;
}
// Recursive adding
if (\is_dir($path)) {
$i = 0;
$glob = \glob($path . DIRECTORY_SEPARATOR . '*');
foreach ($glob as $f) {
$i += $this->addFile($f, $path);
}
return $i;
}
$this->session['add'][] = $path;
echo $this->c['green'], 'File added: ', $this->c[''], $path, "\n";
return 1;
}