diff --git a/.gitignore b/.gitignore index 44102369..77ec8194 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ phpunit.xml tests/adapters/* !tests/adapters/*.dist vendor/ +.idea diff --git a/src/Gaufrette/Adapter/Ftp.php b/src/Gaufrette/Adapter/Ftp.php index 13797677..842261a1 100644 --- a/src/Gaufrette/Adapter/Ftp.php +++ b/src/Gaufrette/Adapter/Ftp.php @@ -352,6 +352,18 @@ protected function createDirectory($directory) } } + /** + * @return string + */ + private function createConnectionUrl() + { + $url = $this->ssl ? 'ftps://' : 'ftp://'; + $url .= $this->username . ':' . $this->password . '@' . $this->host; + $url .= $this->port ? ':' . $this->port : ''; + + return $url; + } + /** * @param string $directory - full directory path * @@ -363,14 +375,27 @@ private function isDir($directory) return true; } - if (!@ftp_chdir($this->getConnection(), $directory)) { - return false; - } - - // change directory again to return in the base directory - ftp_chdir($this->getConnection(), $this->directory); + try { + $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 + ); + } - return true; + // 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); + } } private function fetchKeys($directory = '', $onlyKeys = true)