Skip to content

Commit

Permalink
refactor: change the isDirectory method
Browse files Browse the repository at this point in the history
  • Loading branch information
papac committed Dec 13, 2024
1 parent 9cb52e3 commit d1835f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ env:
FTP_USER: username
FTP_PASSWORD: password
FTP_PORT: 21
FTP_ROOT: /home/vsftpd/username

jobs:
lunix-tests:
Expand Down
17 changes: 12 additions & 5 deletions src/Storage/Service/FTPService.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ public function changePath(?string $path = null)
if ($base_path && (!@ftp_chdir($this->connection, $base_path))) {
throw new RuntimeException('Root is invalid or does not exist: ' . $base_path);
}

ftp_pwd($this->connection);
}

/**
Expand Down Expand Up @@ -483,13 +485,18 @@ public function isFile(string $filename): bool
*/
public function isDirectory(string $dirname): bool
{
$listing = $this->listDirectoryContents();
$original_directory = ftp_pwd($this->connection);

$dirname_info = array_filter($listing, function ($item) use ($dirname) {
return $item['type'] === 'directory' && $item['name'] === $dirname;
});
// Test if you can change directory to $dirname
// suppress errors in case $dir is not a file or not a directory
if (!@ftp_chdir($this->connection, $dirname)) {
return false;
}

return count($dirname_info) !== 0;
// If it is a directory, then change the directory back to the original directory
ftp_chdir($this->connection, $original_directory);

return true;
}

/**
Expand Down

0 comments on commit d1835f2

Please sign in to comment.