Created
January 20, 2022 12:55
-
-
Save Traineratwot/46c4240171f5b608da250494ba3abbbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FTP | |
{ | |
/* | |
* Connection | |
*/ | |
private $connection; | |
/* | |
* connectionString | |
*/ | |
private $connectionString; | |
/** | |
* Instantiate the FTP object. | |
* | |
* @param $host server host | |
* $user username | |
* $pass password | |
* $secured ftp or sftp | |
* | |
* @return bool | |
*/ | |
public function __construct($host, $user, $pass, $secured = FALSE) | |
{ | |
if ($secured === FALSE) { | |
$conn = ftp_connect($host); | |
$login_result = ftp_login($conn, $user, $pass); | |
$this->connection = $conn; | |
$this->connectionString = 'ftp://' . $user . ':' . $pass . '@' . $host; | |
} elseif ($secured === TRUE) { | |
$conn = ftp_ssl_connect($host); | |
$login_result = ftp_login($conn, $user, $pass); | |
$this->connection = $conn; | |
$this->connectionString = 'sftp://' . $user . ':' . $pass . '@' . $host; | |
} else { | |
$this->connection = NULL; | |
$this->connectionString = NULL; | |
} | |
} | |
/** | |
* get the connection. | |
* | |
* @return resource | |
*/ | |
public function getConnection() | |
{ | |
return $this->connection; | |
} | |
/** | |
* check whether the ftp is connected. | |
* | |
* @return bool | |
*/ | |
public function isConnected() | |
{ | |
return (is_resource($this->getConnection())) ? TRUE : FALSE; | |
} | |
/** | |
* get the list of files. | |
* | |
* @param $dir directory | |
* | |
* @return bool | array | |
*/ | |
public function ftpFiles($dir = '/') | |
{ | |
return ($this->isConnected() === TRUE) ? ftp_nlist($this->getConnection(), $dir) : FALSE; | |
} | |
/** | |
* get the current working directory. | |
* | |
* @return bool | array | |
*/ | |
public function pwd() | |
{ | |
return ($this->isConnected() === TRUE) ? ftp_pwd($this->getConnection()) : FALSE; | |
} | |
/** | |
* Change directories. | |
* | |
* @param $dir directory | |
* $new naw name | |
* | |
* @return bool | |
*/ | |
public function chdir($dir = "/") | |
{ | |
return ($this->isConnected() === TRUE) ? ftp_chdir($this->getConnection(), $dir) : FALSE; | |
} | |
/** | |
* Make directory. | |
* | |
* @param $dir directory name | |
* | |
* @return bool | |
*/ | |
public function mkdir($dir) | |
{ | |
return ($this->isConnected() === TRUE) ? ftp_mkdir($this->getConnection(), $dir) : FALSE; | |
} | |
/** | |
* Make nested sub-directories. | |
* | |
* @param string $dirs | |
* | |
* @return Ftp | |
*/ | |
public function mkdirs($dirs) | |
{ | |
if (substr($dirs, 0, 1) == '/') { | |
$dirs = substr($dirs, 1); | |
} | |
if (substr($dirs, -1) == '/') { | |
$dirs = substr($dirs, 0, -1); | |
} | |
$dirs = explode('/', $dirs); | |
$curDir = $this->connectionString; | |
foreach ($dirs as $dir) { | |
$curDir .= '/' . $dir; | |
if (!is_dir($curDir)) { | |
$this->mkdir($dir); | |
} | |
$this->chdir($dir); | |
} | |
return $this; | |
} | |
/** | |
* Remove directory. | |
* | |
* @param $dir directory | |
* | |
* @return bool | |
*/ | |
public function rmdir($dir) | |
{ | |
return ($this->isConnected() === TRUE) ? ftp_rmdir($this->getConnection(), $dir) : FALSE; | |
} | |
/** | |
* Check if file exists. | |
* | |
* @param $dir directory | |
* | |
* @return bool | |
*/ | |
public function fileExists($file) | |
{ | |
return ($this->isConnected() === TRUE) ? ftp_size($this->getConnection(), $file) > 0 : FALSE; | |
} | |
/** | |
* Check is the dir is exists. | |
* | |
* @param $dir directory | |
* | |
* @return bool | |
*/ | |
public function dirExists($dir) | |
{ | |
return ($this->isConnected() === TRUE) ? ftp_rmdir($this->connectionString . $dir) : FALSE; | |
} | |
/** | |
* Get the file. | |
* | |
* @param $local local | |
* $remote remote | |
* $mode mode | |
* | |
* @return bool | |
*/ | |
public function get($local, $remote, $mode = FTP_BINARY) | |
{ | |
return ($this->isConnected() === TRUE) ? ftp_get($this->getConnection(), $local, $remote, $mode) : FALSE; | |
} | |
/** | |
* Rename file. | |
* | |
* @param $old old | |
* $new naw name | |
* | |
* @return bool | |
*/ | |
public function rename($old, $new) | |
{ | |
return ($this->isConnected() === TRUE) ? ftp_rename($this->getConnection(), $old, $new) : FALSE; | |
} | |
/** | |
* Change premission. | |
* | |
* @param $file file | |
* $mode mode | |
* | |
* @return bool | |
*/ | |
public function chmod($file, $mode) | |
{ | |
return ($this->isConnected() === TRUE) ? ftp_chmod($this->getConnection(), $mode, $file) : FALSE; | |
} | |
/** | |
* Delete the files. | |
* | |
* @param $file file you want to delete | |
* | |
* @return bool | |
*/ | |
public function delete($file) | |
{ | |
return ($this->isConnected() === TRUE) ? ftp_delete($this->getConnection(), $file) : FALSE; | |
} | |
/** | |
* Switch the passive mod. | |
* | |
* @param $flag flag | |
* | |
* @return bool | |
*/ | |
public function pasv($flag = TRUE) | |
{ | |
return ($this->isConnected() === TRUE) ? ftp_pasv($this->getConnection(), $flag) : FALSE; | |
} | |
/** | |
* Close the FTP connection. | |
* | |
* @return void | |
*/ | |
public function disconnect() | |
{ | |
if ($this->isConnected()) { | |
ftp_close($this->connection); | |
} | |
} | |
/** | |
* Upload the files. | |
* | |
* @param $files number of files, you want to upload | |
* $root Server root directory or sub | |
* | |
* @return mix-data | |
*/ | |
public function put($files, $server_root = '', $mod = FTP_ASCII) | |
{ | |
if ($this->isConnected() === TRUE) { | |
foreach ($files as $value) { | |
ftp_put($this->getConnection(), $server_root . '/' . $value, $value, $mod); | |
} | |
} else { | |
return FALSE; | |
} | |
} | |
public function file_put_contents($name, $content, $mod = FTP_ASCII) | |
{ | |
if ($this->isConnected() === TRUE) { | |
$fp = tmpfile(); | |
fwrite($fp, $content); | |
rewind($fp); | |
ftp_fput($this->getConnection(), $name, $fp, $mod); | |
fclose($fp); | |
} else { | |
return FALSE; | |
} | |
} | |
public function close(){ | |
return ftp_close($this->getConnection()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment