Document toolboxDocument toolbox

Class Ftphandler.php

This class provides a uniform interface for accessing files on a FTP or SFTP or FTPS server.

Typical use

$conn = new \Microbizz\File\Ftphandler("ftp.microbizz.dk", "ventu", "secret1234", "ftp");
$conn→connect();
$folder = "inbox/";
$files = $conn→list($folder, "*.xml");
foreach ($files as $file) {
  $data = $conn→get("myfile.xml", $folder.$file);
  $conn→delete($folder.$file);
}
$conn->disconnect();

Methods

copies a file from a location to another location.
$local string url it should be sent from.
$remote string url it needs to be sent to.

public function put(string $local = '',string $remote = '')

    this function should add data from a local location to the server connected.
    a connection must already have been made. Refresh server idle time.


Adds specific input. Like a string or something to an already existing file or new file.
$input string of data.
$remote string url it needs to be sent to.
public function putData(string $input = '',string $remote = '') : bool
    this should directly put a string as data to a location.


Gets a file and returns it's data.
$input string of data.
$remote string url it needs to be sent to.
return string data || array datalines || bool false.

public function get(string $local = '', string $remote = '')

    should fetch data to the local position. will also return the content for php to work with.
Refresh server idle time.


Convenience for getting data and deleting
$local string url it should be sent from.
$remote string url it needs to be sent to.
return string $data || bool false.

public function grab(string $local = '',string $remote = '')

     will fetch the data, delete it from the remote location and return the data as a string.

public function delete(string $src) : bool

     will delete the data from the remote location. Refresh server idle time.



$dir string route to folder.
$filter string characters to filter by.
return array of names || false.

public function list(string $dir = '',string $filter = '')

    will give an array of the filenames in a directory in the remote connection.



convenience for checking if server is connectable.
$s string server root route.
$un string username.
$p string password.
$st string server type (ftp, ftps, sftp).

public static function test(string $s,string $un,string $p,string $st)
     will create a connection and return if it works.



public function connect()

    will initiate the connection to server. Server can go into idle mode. This method will only connect to server every IDLE_REFRESH_RATE seconds  but test if it canUpdateConnection every time.


public function canUpdateConection()

    Check if the ftp-connection can/should be updated. Server has a idle mode setting, and therefor connection will "die" and get into a locked mode. When server idle time (no activity) is greater than IDLE_REFRESH_RATE (selected timeperiod to next connections update), then connection can beupdated. Where there is a transfor, we se lastActivity timestamp.


Gets from config and creates a connection from the config.
$module string the module the config can be connected to.
$subject string the config name.
return array with ftp, folderroute, fileroute, ftp type

public static function getFromConfig(string $module = 'invoiceexport', string $subject = 'csvftpconfig')

    convenience to create an ftphandler from config.


Gets the config.
return $config array.

public static function getConfig(string $module, string $subject)

    convenience for getting a ftp getterputter config.


Sets config.
$config array.
$module string the module the config can be connected to.
$subject string the config name.
return true

public static function setConfig(array $config, string $module, string $subject) : bool

    convenience for setting a gtp getter putter config.



Set server type for this connection.
@param string $server_type ftp, ssl or sftp

public function setServerType(string $server_type)



Gets from ftpgetter and creates a connection from the config.
$id integer ftpgetter id.
return array with ftp, folderroute, fileroute, ftp type

public static function ftpGetter(int $id)

    get and create a ftphandler from ftpgetter.



Gets from ftpputter and creates a connection from the config.
$id integer ftpputter id.
return array with ftp, folderroute, fileroute, ftp type

public static function ftpPutter(int $id)

    get and create a ftphandler from ftpputter.