Skip to content

Instantly share code, notes, and snippets.

@omidnasri
Forked from ivandnepr90/FtpHelper.php
Created April 12, 2019 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omidnasri/56f3449487331d2d96d91d924fa2ca1b to your computer and use it in GitHub Desktop.
Save omidnasri/56f3449487331d2d96d91d924fa2ca1b to your computer and use it in GitHub Desktop.
Ftp helper
<?php
/**
* Created by JetBrains PhpStorm.
* User: dn211290sia
* Date: 25.10.14
* Time: 10:00
* To change this template use File | Settings | File Templates.
*/
namespace Common\Utility\GeneralPurpose;
use Common\Utility\GeneralPurpose\Exception\InvalidFtpConnectionException;
/**
* Class FtpHelper for working with ftp. For instance, put
* file to ftp, get list files on ftp server etc.
*
* @package Common\Utility\GeneralPurpose
*/
class FtpHelper
{
/**
* Ftp connection parameters.
*/
private $host = 'your_ip';
private $port = 'port';
private $user = 'user';
private $password = 'password';
/**
* Ftp connection.
*/
private $con = null;
/**
* Constructor. Sets ftp connection if it is necessary.
*
* @param null $host
* @param null $user
* @param null $password
* @param null $port
*/
public function __construct($host = null, $user = null, $password = null, $port = null)
{
if ($host != null) {
$this->connect_to_ftp($host, $port, $user, $password, $port);
}
}
/**
* Set connection parameters.
*
* @param null $host
* @param null $user
* @param null $password
* @param null $port
*/
public function set_connections_parameters($host = null, $user = null, $password = null, $port = null)
{
$this->host = ($host == null) ? $this->host : $host;
$this->user = ($user == null) ? $this->user : $user;
$this->password = ($password == null) ? $this->password : $password;
$this->port = ($password == null) ? $this->port : $port;
}
/**
* Connect to ftp server and authenticate if it is necessary.
*
* @param null $host
* @param null $user
* @param null $password
* @param null $port
* @throws Exception\InvalidFtpConnectionException
*/
public function connect_to_ftp($host = null, $user = null, $password = null, $port = null)
{
$this->set_connections_parameters($host, $port, $user, $password, $port);
$this->con = ftp_connect($this->host, $this->port);
if (!$this->con) {
throw new InvalidFtpConnectionException('invalid ftp connection to ' . $this->host);
}
if (($this->user != null) && ($this->password != null)) {
if (!ftp_login($this->con, $this->user, $this->password)) {
throw new InvalidFtpConnectionException('failed authenticate to ftp host ' . $this->host . ', login ' . $this->user . ', password ' . $this->password);
}
}
}
/**
* Close ftp connection.
*/
function ftp_close()
{
ftp_close($this->con);
}
/**
* Put file to ftp server.
*
* @param $local_file
* @param $remote_file
* @param int $mode
* @throws Exception\InvalidFtpConnectionException
*/
public function put_file_to_ftp($local_file, $remote_file, $mode = FTP_BINARY)
{
if(!file_exists($local_file)) {
throw new InvalidFtpConnectionException('no such file ' . $local_file);
}
if (!$this->con) {
$this->connect_to_ftp();
}
$i = 0;
while (!ftp_put($this->con, $remote_file, $local_file, $mode)) {
if ($i > 4) {
throw new InvalidFtpConnectionException('failed delivery file to ftp server ' . $this->host);
}
$i++;
}
}
/**
* Get file from ftp server.
*
* @param $local_file
* @param $remote_file
* @param int $mode
* @throws Exception\InvalidFtpConnectionException
*/
public function get_file_from_ftp($local_file, $remote_file, $mode = FTP_BINARY)
{
if (!$this->con) {
$this->connect_to_ftp();
}
if(!$this -> is_exist_remote_file($remote_file)) {
throw new InvalidFtpConnectionException('no such file or directory ' . $remote_file . ' on ftp ' . $this->host);
}
$i = 0;
while (!ftp_get($this->con, $local_file, $remote_file, $mode)) {
if ($i > 4) {
throw new InvalidFtpConnectionException('failed delivery file from ftp server ' . $this->host);
}
$i++;
}
}
/**
* Check is exist remote file on ftp server.
*
* @param $file
* @return bool
*/
public function is_exist_remote_file($file) {
if (!$this->con) {
$this->connect_to_ftp();
}
$files_list = $this -> ftp_files_list(dirname($file));
$result = in_array($file, $files_list);
return $result;
}
/**
* Returns files in directory on ftp server.
*
* @param $ftp_dir
* @return array
*/
public function ftp_files_list($ftp_dir) {
if (!$this->con) {
$this->connect_to_ftp();
}
return ftp_nlist($this -> con, $ftp_dir);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment