Skip to content

Instantly share code, notes, and snippets.

@swvitaliy
Created May 24, 2012 21:15
Show Gist options
  • Save swvitaliy/2784277 to your computer and use it in GitHub Desktop.
Save swvitaliy/2784277 to your computer and use it in GitHub Desktop.
ftp client
<?php
class Ftp_Exception extends Exception
{
}
class Ftp
{
protected static function trim_path($path) {
return trim($path, "\t\n\r\0 /");
}
protected static function error()
{
$err = error_get_last();
if (is_null($err))
return false;
throw new Ftp_Exception($err['message'], $err['type']);
}
public static function ctor($ftp_server, $ftp_user_name, $ftp_user_pass, $root_dir, $do_connect = true)
{
return new Ftp($ftp_server, $ftp_user_name, $ftp_user_pass, $root_dir, $do_connect);
}
private $ftp_server;
private $ftp_user_name;
private $ftp_user_pass;
private $root_dir;
private $curr_dir;
private $conn_id = null;
private $login_result;
protected function set_dir()
{
if (!@ftp_chdir($this->conn_id, $this->curr_dir))
self::error();
return $this;
}
public function __construct($ftp_server, $ftp_user_name, $ftp_user_pass, $root_dir, $do_connect = true)
{
$this->ftp_server = $ftp_server;
$this->ftp_user_name = $ftp_user_name;
$this->ftp_user_pass = $ftp_user_pass;
$this->curr_dir = self::trim_path($root_dir) . '/';
$this->root_dir = '/' . $this->curr_dir;
if ($do_connect) {
$this->open();
$this->change_dir('/');
}
}
public function __destruct()
{
$this->close();
}
public function open()
{
$this->conn_id = @ftp_connect($this->ftp_server);
if ($this->conn_id) {
$this->login_result =
@ftp_login($this->conn_id, $this->ftp_user_name, $this->ftp_user_pass);
}
if ((!$this->conn_id) || (!$this->login_result))
self::error();
return $this;
}
public function close()
{
if ($this->conn_id) {
if (@ftp_close($this->conn_id)) {
self::error();
}
$this->conn_id = null;
}
return $this;
}
public function cur_dir()
{
return @ftp_pwd($this->conn_id) ? : self::error();
}
public function snap_dir ()
{
return $this->curr_dir;
}
public function change_dir($path)
{
if ($path=='..') {
$cp = self::trim_path($this->curr_dir);
$rp = self::trim_path($this->root_dir);
if (strlen($cp)==strlen($rp))
return $this;
$lastDot = strrpos($cp, '/');
$this->curr_dir = substr($this->curr_dir, 0, $lastDot + 1) . '/';
} else {
if ($path[0]=='/') {
$path = self::trim_path($path);
$this->curr_dir = $this->root_dir .
($path ? $path . '/' : '');
} else
$this->curr_dir .= self::trim_path($path) . '/';
}
return $this->set_dir();
}
public function make_dir($dir_name)
{
if (!@ftp_mkdir($this->conn_id, $this->curr_dir . $dir_name)) {
self::error();
}
}
public function try_mk_dir ($dir_name, &$result = null)
{
try {
$result = @ftp_mkdir($this->conn_id, $this->curr_dir . $dir_name);
} catch (Exception $e) { $result = false; }
return $this;
}
public function exist_dir($dir)
{
$origin = $this->cur_dir();
if (@ftp_chdir($this->conn_id, $dir)) {
ftp_chdir($this->conn_id, $origin);
return true;
}
return false;
}
public function send_file($remote_file, $local_file, $mode = FTP_BINARY)
{
if ($remote_file[0]!=='/')
$remote_file = $this->curr_dir . $remote_file;
if (!@ftp_put($this->conn_id, $remote_file, $local_file, $mode))
self::error();
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment