Connect FTP using PHP script

by kalai 2010-06-20 16:38:09


CLASS PHP_FTP{

VAR $server='';
VAR $username='';
VAR $password='';
VAR $port=21;
VAR $remote_dir='';

FUNCTION PHP_FTP($server, $username='anonymous', $password='e@mail.com', $port=21){
$this->server=$server;
$this->username=$username;
$this->password=$password;
$this->port=$port;
}

//exterior

FUNCTION send($filename='', $save_as='', $passive=TRUE){
$conn=$this->return_connection() or DIE;
@FTP_PASV($conn, $passive);

$this->set_remote_dir(FTP_PWD($conn));
IF(!FTP_PUT($conn, $save_as, $filename, FTP_BINARY)){
@FTP_QUIT($this->conn);
RETURN FALSE;
}ELSE{
@FTP_QUIT($this->conn);
RETURN TRUE;
}
RETURN TRUE;
}


FUNCTION cfolder($idd,$dir)
{
print "$dir ----";
$conn=$this->return_connection() or DIE;
@FTP_PASV($conn, $passive);
$this->set_remote_dir(FTP_PWD($conn));

if (ftp_mkdir($conn, $dir)) {
echo "successfully created $dirn";
}
RETURN TRUE;

}

FUNCTION get($filename='', $save_as='', $passive=TRUE){
$conn=$this->return_connection() or DIE;
@FTP_PASV($conn, $passive);
$this->set_remote_dir(FTP_PWD($conn));
IF(!FTP_GET($conn, $save_as, $this->remote_dir.$filename, FTP_BINARY)){
@FTP_QUIT($this->conn);
RETURN FALSE;
}ELSE{
@FTP_QUIT($this->conn);
RETURN TRUE;
}
}

FUNCTION kill(){
IF($this->conn)
$this->disconnect();
UNSET($this);
}

//interior
FUNCTION return_connection(){
$conn_id = @FTP_CONNECT($this->server, $this->port) or DIE("Could not connect to FTP");
$login_result = @FTP_LOGIN($conn_id, $this->username, $this->password) or DIE("Could not login to FTP");
RETURN $conn_id;
}

FUNCTION set_remote_dir($dir){
$x = SUBSTR($dir, (STRLEN($dir)-1));
IF($x != "/" && $x != "\")
$dir.="/";
$this->remote_dir=$dir;
}
}



$f=new PHP_FTP('withfriendship.com', 'username', 'pwd', 21); //optional port as 4th arg
$f->cfolder('12', "public_html/images/$aa/$sid"); //yes, tested on Windows
print "file to be uploaded is upload/$file1/$files";
$f->send("$nname", "public_html/images/$aa/$sid/$files"); //yes, tested on Windows
$f->kill();
?>

Tagged in:

1754
like
0
dislike
0
mail
flag

You must LOGIN to add comments