Skip to content
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
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,29 @@ to the require section of your composer.json.

## Getting Started

Connect to a server FTP :
Configuration in config/main.php

```php
$ftp = new \yii2mod\ftp\FtpClient();
$host = 'ftp.example.com';
$ftp->connect($host);
$ftp->login($login, $password);
'components' => [
...
'ftp' => [
'class' => \yii2mod\ftp\components\FtpClient::class,
'host' => 'ftp.site.com',
'user' => 'root',
'password' => '123',
'port' => 21,
'ssl' => true,
'passive' => true,
'timeout' => 90,
],
...
];
```

OR

Connect to a server FTP via SSL (on port 22 or other port) :
Connect to a server FTP :

```php
$ftp = new \yii2mod\ftp\FtpClient();
$host = 'ftp.example.com';
$ftp->connect($host, true, 22);
$ftp->login($login, $password);
$ftp = \Yii::$app->ftp->connect();
```

Note: The connection is implicitly closed at the end of script execution (when the object is destroyed). Therefore it is unnecessary to call `$ftp->close()`, except for an explicit re-connection.
Expand Down
67 changes: 67 additions & 0 deletions components/FtpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace yii2mod\ftp\components;

use yii\base\Component;
use yii2mod\ftp\FtpClient as Ftp;

/**
* Class Ftp
* @package frontend\components
*/
class FtpClient extends Component
{
/**
* @var
*/
public $host;

/**
* @var
*/
public $user;

/**
* @var
*/
public $password;

/**
* @var int
*/
public $port = 21;

/**
* @var bool
*/
public $ssl = false;

/**
* @var bool
*/
public $passive = false;

/**
* @var int
*/
public $timeout = 90;

public function init()
{
parent::init();
}

/**
* @return Ftp
* @throws \yii2mod\ftp\FtpException
*/
public function connect()
{
$ftp = new Ftp();
$ftp->connect($this->host, $this->ssl, $this->port, $this->timeout);
$ftp->login($this->user, $this->password);
$ftp->pasv($this->passive);

return $ftp;
}
}