Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for the isDir function in the FTP Adapter #607

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
38 changes: 33 additions & 5 deletions src/Gaufrette/Adapter/Ftp.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,18 @@ protected function createDirectory($directory)
}
}

/**
* @return string
*/
private function createConnectionUrl()
{
$url = $this->ssl ? 'sftp://' : 'ftp://';
Romain marked this conversation as resolved.
Show resolved Hide resolved
$url .= $this->username . ':' . $this->password . '@' . $this->host;
$url .= $this->port ? ':' . $this->port : '';

return $url;
}

/**
* @param string $directory - full directory path
*
Expand All @@ -366,12 +378,28 @@ private function isDir($directory)
return true;
}

if (!@ftp_chdir($this->getConnection(), $directory)) {
nicolasmure marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
try {
$chDirResult = false;
Romain marked this conversation as resolved.
Show resolved Hide resolved
$chDirResult = ftp_chdir($this->getConnection(), $this->directory);

// change directory again to return in the base directory
ftp_chdir($this->getConnection(), $this->directory);
return $chDirResult;
} catch (\Exception $e) {
// is_dir is only available in passive mode.
// See https://php.net/manual/en/wrappers.ftp.php for more details.
if (!$this->passive) {
throw new \RuntimeException(
\sprintf('Not able to determine whether "%s" is a directory or not. Please try again using a passive FTP connection if your backend supports it, by setting the "passive" option of this adapter to true.', $directory),
$e->getCode(),
$e
);
}

// change directory again to return in the base directory
ftp_chdir($this->getConnection(), $this->directory);
// Build the FTP URL that will be used to check if the path is a directory or not
$url = $this->createConnectionUrl();
return @is_dir($url . $directory);
}

return true;
Romain marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down