From acc50e2fab99783832c793edcb42d1d22d49b591 Mon Sep 17 00:00:00 2001 From: ghost Date: Sat, 25 Nov 2023 02:28:25 +0200 Subject: [PATCH] initial commit --- .gitignore | 1 + composer.json | 11 +++++ src/Ftp.php | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Ftp.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ab28d34 --- /dev/null +++ b/composer.json @@ -0,0 +1,11 @@ +{ + "name": "yggverse/ftp", + "type": "library", + "license": "MIT", + "autoload": { + "psr-4": { + "Yggverse\\Ftp\\": "src/" + } + }, + "require": {} +} diff --git a/src/Ftp.php b/src/Ftp.php new file mode 100644 index 0000000..a9ae8f0 --- /dev/null +++ b/src/Ftp.php @@ -0,0 +1,117 @@ +_passive = $passive; + } + + public function connect(string $host, + int $port, + mixed $login = null, + mixed $password = null, + string $directory = '/', + int $timeout = 90, + bool $passive = false) { + + if (!$this->_connection = ftp_connect($host, $port, $timeout)) { + + return false; + } + + if (!empty($login) && !empty($password)) { + + if (!ftp_login($this->_connection, $login, $password)) { + + return false; + } + } + + if ($passive && !ftp_pasv($this->_connection, $this->_passive)) { + + return false; + } + + return ftp_chdir($this->_connection, $directory); + } + + public function delete(string $target) { + + return ftp_delete($this->_connection, $target); + } + + public function copy(string $source, string $target) { + + return ftp_put($this->_connection, $target, $source); + } + + public function get(string $source, string $target) { + + return ftp_get($this->_connection, $target, $source); + } + + public function mkdir(string $name, bool $recursive = false) { + + if ($recursive) { + + $path = []; + + foreach ((array) explode('/', trim($name, '/')) as $directory) { + + $path[] = $directory; + + @ftp_mkdir($this->_connection, implode('/', $path)); + } + + } else { + + @ftp_mkdir($this->_connection, $name); + } + } + + public function size(string $target) { + + if (-1 !== $size = ftp_size($this->_connection, $target)) { + + return $size; + + } + + return false; + } + + public function nlist(string $path) { + + return ftp_nlist($this->_connection, $path); + } + + public function nlistr(string $path) { + + $result = []; + + foreach (ftp_nlist($this->_connection, $path) as $line) { + + if (ftp_size($this->_connection, $line) == -1) { + + $result = array_merge($result, $this->nlistr($line)); + + } else{ + + $result[] = $line; + } + } + + return $result; + } + + public function close() { + + return ftp_close($this->_connection); + } +} \ No newline at end of file