Andy
Andy

Reputation: 21

Codeigniter 4 send uploaded file to FTP Server

Please help, i want to send my uploaded file to my other server trough FTP. I cannot found any documentation about FTP in CodeIgniter 4 official documentation page. Can i use CI 3 FTP library for this ?

Right now i have successfully uploaded file in my web server.

Upvotes: 0

Views: 1956

Answers (1)

marcogmonteiro
marcogmonteiro

Reputation: 2172

Codeigniter 4 does not provide a FTP library. You can try and adapt the old library to work on CI4 but what I would do is use a composer package to do this:

https://packagist.org/packages/nicolab/php-ftp-client

You can just install that library via composer and then you would use it like so:

Install the library with composer:

require nicolab/php-ftp-client

Connect to a server FTP :

$ftp = new \FtpClient\FtpClient();
$ftp->connect($host);
$ftp->login($login, $password);

OR

Connect to a server FTP via SSL (on port 990 or another port) :

$ftp = new \FtpClient\FtpClient();
$ftp->connect($host, true, 990);
$ftp->login($login, $password);

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.

Usage Upload all files and all directories is easy :

// upload with the BINARY mode
$ftp->putAll($source_directory, $target_directory);

// Is equal to
$ftp->putAll($source_directory, $target_directory, FTP_BINARY);

// or upload with the ASCII mode
$ftp->putAll($source_directory, $target_directory, FTP_ASCII); 

If you still want to use the codeigniter 3 library, from what I see that should be pretty easy to do. Just add it to your app/Libraries folder.

Then you would need to change 2 things:

  • Namespace the library;
  • Change the _errors function that uses the CI 3 instance;

Upvotes: 1

Related Questions