Skip to content

Instantly share code, notes, and snippets.

@martinmev
Created December 27, 2011 22:00
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 martinmev/1525296 to your computer and use it in GitHub Desktop.
Save martinmev/1525296 to your computer and use it in GitHub Desktop.
Whole ftp.class.php with SSL
<?php
//////////////////////////////////////////////////
//ftp class
//author:paul.ren
//e-mail:rsr_cn@yahoo.com.cn
//website:www.yawill.com
//create:2004-6-23 09:22
//modify: 2011-12-30 - Martin Mevald, www.mevald.cz - added SSL connection
//////////////////////////////////////////////////
class ClsFTP{
var $host = "localhost"; //FTP HOST, prefix "!" - use SSL
var $port = "21"; //FTP port
var $user = "Anonymous";//FTP user
var $pass = "Email"; //FTP password
var $link_id = ""; //FTP hand
var $is_login = ""; //is login
var $debug = 1;
var $local_dir = ""; //local path for upload or download
var $rootdir = ""; //FTP root path of FTP server
var $dir = "/"; //FTP current path
function ClsFTP($user="Anonymous",$pass="Email",$host="localhost",$port="21"){
if($host) $this->host = $host;
if($port) $this->port = $port;
if($user) $this->user = $user;
if($pass) $this->pass = $pass;
$this->login();
$this->rootdir = $this->pwd();
$this->dir = $this->rootdir;
}
function halt($msg,$line=__LINE__){
echo "FTP Error in line:$line<br/>\n";
echo "FTP Error message:$msg<br/>\n";
exit();
}
function connLogin($conn){ // $conn - resource from ftp_connect / ftp_ssl_connect
if(!$this->link_id){
if (!($this->link_id = $conn)) {
return false; //$this->halt("can not connect to host:$this->host:$this->port",__LINE__);
}
}
if(!$this->is_login){
if (!($this->is_login = ftp_login($this->link_id, $this->user, $this->pass))) {
return false;//or $this->halt("ftp login faild.invaid user or password",__LINE__);
}
}
return true;
}
function login() { // host: prefix "!" - use SSL
if (substr($this->host,0,1)=='!') {
if (!function_exists('ftp_ssl_connect')) {
return false;
}
$host=substr($this->host,1,strlen($this->host));
$conn = ftp_ssl_connect($host,$this->port,30);
return ($this->connLogin($conn));
} else {
return $this->connLogin(ftp_connect($this->host,$this->port, 30));
}
}
function systype(){
return ftp_systype($this->link_id);
}
function pwd(){
$this->login();
$dir = ftp_pwd($this->link_id);
$this->dir = $dir;
return $dir;
}
function cdup(){
$this->login();
$isok = ftp_cdup($this->link_id);
if($isok) $this->dir = $this->pwd();
return $isok;
}
function cd($dir){
$this->login();
$isok = ftp_chdir($this->link_id,$dir);
if($isok) $this->dir = $dir;
return $isok;
}
function nlist($dir=""){
$this->login();
if(!$dir) $dir = ".";
$arr_dir = ftp_nlist($this->link_id,$dir);
return $arr_dir;
}
function rawlist($dir="/"){
$this->login();
$arr_dir = ftp_rawlist($this->link_id,'-a '.$dir);
return $arr_dir;
}
function mkdir($dir){
$this->login();
return @ftp_mkdir($this->link_id,$dir);
}
function file_size($file){
$this->login();
$size = ftp_size($this->link_id,$file);
return $size;
}
function chmod($file,$mode=0666){
$this->login();
return ftp_chmod($this->link_id,$file,$mode);
}
function rename($old_file_name,$new_file_name){
$this->login();
return ftp_rename($this->link_id,$old_file_name,$new_file_name);
}
function delete($remote_file){
$this->login();
return ftp_delete($this->link_id,$remote_file);
}
function get($local_file,$remote_file,$mode=FTP_BINARY){
$this->login();
return ftp_get($this->link_id,$local_file,$remote_file,$mode);
}
function put($remote_file,$local_file,$mode=FTP_BINARY){
$this->login();
return ftp_put($this->link_id,$remote_file,$local_file,$mode);
}
function put_string($remote_file,$data,$mode=FTP_BINARY){
$this->login();
$tmp = "/tmp";//ini_get("session.save_path");
$tmpfile = tempnam($tmp,"tmp_");
$fp = @fopen($tmpfile,"w+");
if($fp){
fwrite($fp,$data);
fclose($fp);
}else return 0;
$isok = $this->put($remote_file,$tmpfile,FTP_BINARY);
@unlink($tmpfile);
return $isok;
}
function p($msg){
echo "<pre>";
print_r($msg);
echo "</pre>";
}
function close(){
@ftp_quit($this->link_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment