PHP code example of kodhe / ftp

1. Go to this page and download the library: Download kodhe/ftp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kodhe / ftp example snippets


// Di application/config/config.php atau bootstrap
spl_autoload_register(function ($class) {
    $prefix = 'Kodhe\\Ftp\\';
    $base_dir = APPPATH . 'libraries/Ftp/src/';
    
    if (strpos($class, $prefix) === 0) {
        $relative_class = substr($class, strlen($prefix));
        $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
        if (file_exists($file)) {
            

class BackupController extends CI_Controller {
    
    public function upload_backup() {
        $this->load->library('ftp');
        
        $config = [
            'hostname' => 'ftp.example.com',
            'username' => 'user',
            'password' => 'pass',
            'port'     => 21,
            'passive'  => true,
            'debug'    => true,
        ];
        
        if ($this->ftp->connect($config)) {
            // Upload file
            $this->ftp->upload('/local/path/backup.zip', '/remote/backup.zip', 'binary');
            
            // List files
            $files = $this->ftp->list_files('/remote/');
            
            // Delete file
            $this->ftp->delete_file('/remote/old_backup.zip');
            
            $this->ftp->close();
        }
    }
}

use Kodhe\Ftp\Ftp;
use Kodhe\Ftp\Connection\FtpConnection;

// Dengan constructor
$ftp = new Ftp([
    'hostname' => 'ftp.example.com',
    'username' => 'user',
    'password' => 'pass',
    'ssl'      => false,
    'passive'  => true,
]);

// Atau manual connect
$ftp = new Ftp();
$ftp->connect([
    'hostname' => 'ftp.example.com',
    'username' => 'user', 
    'password' => 'pass',
]);

// Upload dengan auto-detect mode
$ftp->upload('/local/file.txt', '/remote/file.txt'); // ASCII
$ftp->upload('/local/image.jpg', '/remote/image.jpg'); // Binary

// Download
$ftp->download('/remote/file.txt', '/local/file.txt');

// Rename/Move
$ftp->rename('/remote/old.txt', '/remote/new.txt');
$ftp->move('/remote/file.txt', '/remote/folder/file.txt');

// Create directory
$ftp->mkdir('/remote/new_folder', 0755);

// Change directory
$ftp->changedir('/remote/folder');

// List files
$files = $ftp->list_files('/remote');

// Delete
$ftp->delete_file('/remote/file.txt');
$ftp->delete_dir('/remote/folder');

// Chmod
$ftp->chmod('/remote/file.txt', 0644);

// Close connection
$ftp->close();

use Kodhe\Ftp\Ftp;

$ftp = new Ftp([
    'hostname' => 'secure.example.com',
    'username' => 'user',
    'password' => 'pass',
    'ssl'      => true,  // Enable SSL
    'port'     => 990,   // Port FTPS
    'passive'  => true,
]);

ftp/
├── src/
│   ├── Ftp.php                       # Main class (Facade)
│   ├── Contracts/
│   │   ├── FtpInterface.php          # Interface utama
│   │   └── ConnectionInterface.php   # Interface koneksi
│   ├── Connection/
│   │   ├── FtpConnection.php         # FTP biasa
│   │   └── FtpSslConnection.php      # FTP over SSL
│   ├── Operations/
│   │   ├── FileOperations.php        # Upload, download, delete, rename, chmod
│   │   └── DirectoryOperations.php   # Mkdir, list_files, delete_dir, changedir
│   └── Validation/
│       └── ModeResolver.php          # Auto-detect mode ascii/binary
├── tests/
├── composer.json
└── README.md