Jacek Kowalski
2023-11-17 1e5643c19c32a84234a3e8fa00c3ca50511d54cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
class FtpConnection {
    private static $instances = [];
    private $connection;
    
    static function create(string $host, int $port=21, string $user='anonymous', string $pass='anonymous') : FtpConnection {
        $key = $host."\0".$port."\0".$user."\0".$pass;
        if(!isset(self::$instances[$key])) {
            self::$instances[$key] = new FtpConnection($host, $port, $user, $pass);
        }
        return self::$instances[$key];
    }
    
    private function __construct(string $host, int $port=21, string $user, string $pass) {
        $this->connection = ftp_connect($host, $port, 10);
        if($this->connection === FALSE) {
            throw new Exception('FTP connection failed');
        }
        if(!ftp_login($this->connection, $user, $pass)) {
            throw new Exception('FTP login failed');
        }
        if(!ftp_pasv($this->connection, TRUE)) {
            throw new Exception('Passive FTP request failed');
        }
    }
    
    public function __destruct() {
        ftp_close($this->connection);
    }
    
    public function size(string $file) : int {
        $remoteSize = ftp_size($this->connection, $file);
        if($remoteSize < 0) {
            throw new Exception('FTP file size fetch failed');
        }
        return $remoteSize;
    }
    
    
    public function mdtm(string $file) : int {
        $remoteTime = ftp_mdtm($this->connection, $file);
        if($remoteTime < 0) {
            throw new Exception('FTP modification time fetch failed');
        }
        return $remoteTime;
    }
    
    public function get(string $local_file, string $remote_file, int $mode = FTP_BINARY, int $resumepos = 0) : bool {
        $result = ftp_get($this->connection, $local_file, $remote_file, $mode, $resumepos);
        if($result === FALSE) {
            throw new Exception('FTP file get failed');
        }
        return $result;
    }
}