Skip to content

Commit

Permalink
Fix listDirectory() not always falling back to basic directory listing
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Dec 5, 2019
1 parent aa3b848 commit e9499c7
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/FtpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,25 @@ public function listDirectory(string $directory) : array
{
$this->throwIfNotConnected();

$records = false;

try {
$records = $this->call(true, 'ftp_mlsd', $this->conn, $directory);
} catch (FtpException $e) {
if (substr($e->getMessage(), 0, 3) === '500') {
// MLSD command not supported
return $this->basicListDirectory($directory);
} else {
// Other error, re-throw
if (substr($e->getMessage(), 0, 3) !== '500') {
// MLSD command supported, but an error occurred
throw $e;
} else {
// MLSD command not supported; ignore the exception
}
}

if ($records === false) {
throw new FtpException('Unable to get file list.');
// Note: ftp_mlsd() may return false without a warning, even when the server returns a 500 response
// to the MLSD command (not supported); therefore, at this point we cannot guarantee that we got there
// because of the MLSD command not being supported, or because an unknown error occurred; in any case, we
// have no other choice but to fall back to the basic directory listing, whatever the error.
return $this->basicListDirectory($directory);
}

$result = [];
Expand Down

0 comments on commit e9499c7

Please sign in to comment.