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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ phpunit.xml
tests/adapters/*
!tests/adapters/*.dist
vendor/
.idea
39 changes: 32 additions & 7 deletions src/Gaufrette/Adapter/Ftp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -363,14 +375,27 @@ private function isDir($directory)
return true;
}

if (!@ftp_chdir($this->getConnection(), $directory)) {
nicolasmure marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down