Skip to content

Commit

Permalink
Always strip path from FtpFileInfo::$name
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Dec 5, 2019
1 parent e9499c7 commit 84f5890
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/FtpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function listDirectory(string $directory) : array
*/
private function mlsdFactsToFileInfo(array $facts) : ?FtpFileInfo
{
$name = $facts['name'];
$name = $this->stripPathFromFileName($facts['name']);

if ($name === '.' || $name === '..') {
return null;
Expand Down Expand Up @@ -290,6 +290,8 @@ private function basicListDirectory(string $directory) : array
$result = [];

foreach ($files as $file) {
$file = $this->stripPathFromFileName($file);

if ($file === '.' || $file === '..') {
continue;
}
Expand All @@ -303,6 +305,28 @@ private function basicListDirectory(string $directory) : array
return $result;
}

private function stripPathFromFileName(string $path) : string
{
$pos1 = strrpos($path, '/');
$pos2 = strrpos($path, '\\');

if ($pos1 === false && $pos2 === false) {
return $path;
}

if ($pos1 !== false) {
if ($pos2 !== false) {
$pos = max($pos1, $pos2);
} else {
$pos = $pos1;
}
} else {
$pos = $pos2;
}

return substr($path, $pos + 1);
}

/**
* Lists all files in the given directory and its subdirectories.
*
Expand Down

0 comments on commit 84f5890

Please sign in to comment.