Connect with us

solution

PHP FTP Class

One of the many common things we do as PHP developers is ftp files from one server to another. There are many times when I have needed this functionality and PHP has a wonderful API for ftp. Here is a quick wrapper class that I wrote that encapsulates much of the functionality for ftp:

<?php
class ftp
{
	private $conn_id;
	private $host;
	private $username;
	private $password;
	private $port;
	public  $timeout = 90;
	public  $passive = false;
	public  $ssl 	 = false;
	public  $system_type = '';
 
 
	public function __construct($host, $username, $password, $port = 21)
	{
		$this->host     = $host;
		$this->username = $username;
		$this->password = $password;
		$this->port     = $port;
	}
 
	public function connect()
	{
		if ($this->ssl == false)
		{
			$this->conn_id = ftp_connect($this->host, $this->port);
		}
		else
		{
			if (function_exists('ftp_ssl_connect'))
			{
				$this->conn_id = ftp_ssl_connect($this->host, $this->port);
			}
			else
			{
				return false;	
			}
		}
 
		$result = ftp_login($this->conn_id, $this->username, $this->password);
 
		if ($result == true)
		{
			ftp_set_option($this->conn_id, FTP_TIMEOUT_SEC, $this->timeout);
 
			if ($this->passive == true)
			{
				ftp_pasv($this->conn_id, true);
			}
			else
			{
				ftp_pasv($this->conn_id, false);
			}
 
			$this->system_type = ftp_systype($this->conn_id);
 
			return true;
		}
		else
		{
			return false;
		}
	}
 
	public function put($local_file_path, $remote_file_path, $mode = FTP_ASCII)
	{
		if (ftp_put($this->conn_id, $remote_file_path, $local_file_path, $mode))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
 
	public function get($local_file_path, $remote_file_path, $mode = FTP_ASCII)
	{
		if (ftp_get($this->conn_id, $local_file_path, $remote_file_path, $mode))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
 
	public function chmod($permissions, $remote_filename)
	{
		if ($this->is_octal($permissions))
		{
			$result = ftp_chmod($this->conn_id, $permissions, $remote_filename);
			if ($result)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			throw new Exception('$permissions must be an octal number');
		}
	}
 
	public function chdir($directory)
	{
		ftp_chdir($this->conn_id, $directory);
	}
 
	public function delete($remote_file_path)
	{
		if (ftp_delete($this->conn_id, $remote_file_path))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
 
	public function make_dir($directory)
	{
		if (ftp_mkdir($this->conn_id, $directory))
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
 
	public function rename($old_name, $new_name)
	{
		if (ftp_rename($this->conn_id, $old_name, $new_name))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
 
	public function remove_dir($directory)
	{
		if (ftp_rmdir($this->conn_id, $directory))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
 
	public function dir_list($directory)
	{
		$contents = ftp_nlist($this->conn_id, $directory);
		return $contents;
	}
 
	public function cdup()
	{
		ftp_cdup($this->conn_id);
	}
 
	public function current_dir()
	{
		return ftp_pwd($this->conn_id);
	}
 
	private function is_octal($i) 
	{
    	return decoct(octdec($i)) == $i;
	}
 
	public function __destruct()
	{
		if ($this->conn_id)
		{
			ftp_close($this->conn_id);
		}
	}
}
?>

This is about as straight forward as it gets so I won’t be long winded about it lol. To use it simply use the following code:

$server = new ftp('domain.com', 'username', 'password');
 
if ($server->connect())
{
	$server->chdir('public_html');
	echo $server->current_dir();
}
else
{
	echo 'could not connect';
}

This simply connects to the server and echos’ out the current directory.

Functions

Here are a few functions to use:

  • connect() – The main function you use to connect to the ftp site
  • put($local_file_path, $remote_file_path, [optional] $mode) – The function you use to send a file from the local server to the remote server. The mode is set to FTP_ASCII, but if you are sending something other than a text file you will want to specify FTP_BINARY.
  • get() – this function is the exact same as put() except it does the opposite. It downloads from the remote server to the local server.
  • chmod($permissions, $remote_filename) – this function will change permissions on the remote server. $permissions is a parameter that accepts an octal numeric value.
  • chdir($directory) – this function changes the current directory that we are working in.
  • delete($remote_file_path) – Deletes a file off of the remote directory.
  • make_dir($directory) – creates a new directory on the remote server.
  • rename($old_name, $new_name) – renames a file or directory.
  • remove_dir($directory) – removes a directory from the remote server
  • dir_list() – returns an array of the current directory’s files and folders.
  • cd_up() – moves the current working directory up one level.
  • current_dir() – returns a string of the current working directory.

Security

A word on security. FTP – unfortunately – sends the username / password as clear text (no encryption). If a hacker is sniffing your server and is able to retrieve the packet with the username and password there is no decryption necessary. Fortunately there is ftps (which is basically is the same thing as https). If you specify $server->ssl = true; before you connect this will force the ftp class to connect securely as long as your server and the remote server support it. This usually does not work on Windows/PHP environments especially if you are on a shared Windows/PHP environment. If you manually compile PHP you can use this functionality otherwise you will not be able to use the secure connect.

Continue Reading
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More in solution

    About Me:

    Szabi Kisded

    Hey there, I'm Szabi. At 30 years old, I quit my IT job and started my own business and became a full time WordPress plugin developer, blogger and stay-at-home dad. Here I'm documenting my journey earning an online (semi)passive income. Read more

    Sign up for my newsletter and get the YouTube Caption Scraper WordPress plugin for free
    (worth 29$)!

    All My Plugins In A Bundle:

    My AutoBlogging Plugins:

    My Online Courses:

    A Theme I Recommend:

    Featured Posts:

    To Top